图像将随页面一起滚动
Image becomes scrollabe with the page
我有这样的代码,用于从第三方系统提供图像:
var bbimagebannerfile = "";
document.write("<a href='"%%__REDIRECT%%'" target='"_blank'"><img src='"" + bbimagebannerfile + "'" border='"0'" /></a>");
此图像位于页面底部。我想做的是,一旦用户到达页面的位置,就让这个图像与页面一起滚动,当用户向上滚动时,这个图像就会再次变为静态。有人能帮我吗?
仅举一个例子:
<div id="fixed-image"></div>
如果我们假设上面的HTML是您正在提供的内容。你可以添加这个css:
#fixed-image {
width: 100%;
height: 100px;
background-color: red;
position: fixed;
bottom: 0;
}
在您的情况下,可以尝试添加具有上述样式的类属性。
JS Fiddle:http://jsfiddle.net/df6tmLrg/
另一个使用javascript的演示:http://codepen.io/FakeHeal/pen/qEgxKN(codepen,因为jsfiddle不允许document.write
)
var bbimagebannerfile = "https://avatars2.githubusercontent.com/u/1038697?v=3&s=460";
document.write("<a href='"%%__REDIRECT%%'" target='"_blank'" class='"bottom-fixed'"><img src='"" + bbimagebannerfile + "'" border='"0'" width="100" /></a>");
和css:
.bottom-fixed {
width: 100%;
height: 100px;
position: fixed;
bottom: 0;
}
更新:如果使用jQuery
,则可以使用.scroll()
:
var t = $("#fixed-image").offset().top;
$(document).scroll(function(){
if($(this).scrollTop() > t)
{
$("#fixed-image")
.css('position', 'fixed') //we change the position to fixed
.css('top',0); // and the top to zero
} else {
$("#fixed-image")
.css('position', 'static') //we change the position to fixed
.css('top',0); // and the top to zero
}
});
下面是一个演示:http://jsfiddle.net/df6tmLrg/2/
相关文章:
- 将两个ext.TabPanels滚动到一起
- "此网站似乎使用滚动链接定位效果.这可能不能很好地与异步平移一起工作;
- 图像将随页面一起滚动
- 与主要内容一起滚动的边栏
- 是否可以将滚动条系在一起
- iframe允许在与openzoom一起使用时在包含页面中滚动
- 水平滚动Marquee,当函数被调用时,我的文本坚持聚集在一起
- 滚动到不与溢出一起工作的顶部脚本:滚动
- fullPage.js滚动条:true使浏览器随deelay一起移动
- 要随索引页一起加载的滚动更新,并在 iframe 加载来自链接的内容时消失
- 使微小的滚动条与不断变化的动画内容一起工作
- 如何在UI中与React JS一起进行无限滚动
- Bootstrap滚动间谍和粘性导航不能一起工作
- 当autoheight与fullpage.js中的自动滚动一起使用时,URL和导航链接不会更改
- 可以't让滚动动画与jquery一起工作
- 如何分离DataTable Header以便它可以与网页一起滚动
- 如何使用JQuery与Bootstrap 3模式一起制作基于滚动的动画
- 与水平页面一起滚动的菜单
- 自动HTML页面刷新保留滚动位置,与锚一起工作
- 平滑滚动脚本和滑块不能一起工作