如何通过jquery从上到下改变图像

时间:2014-08-29 09:35:02

标签: javascript jquery

我在jsfiddle中有广告代码: Fiddle

当我滚动页面时(或更改填充/边距底部),如何更改2个广告以始终显示底部

HTML

<div class="wrapper">
    <div class="adsl">
        <image alt='scoll1-2tieng' src='http://www.maydoduonghuyet.com.vn/uploads/partners/scoll1_2tieng.gif' width='120' height='300' />
    </div>
    <div class="adsr">
        <image alt='scoll2-hdsd' src='http://www.maydoduonghuyet.com.vn/uploads/partners/scoll2_hdsd.jpg' width='120' height='300' />
    </div>
</div>

的jQuery

$(document).ready(function() {

var sidebar   = $(".adsl");
    var offset    = sidebar.offset();
    var topPadding = 15;
    $(window).scroll(function() {
        if ($(window).scrollTop() > offset.top) {
            sidebar.stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding
            });
        } else {
            sidebar.stop().animate({
                marginTop: 0
            });
        }
    });    
var sidebar2   = $(".adsr");
    var offset2    = sidebar2.offset();
    var topPadding2 = 15;
    $(window).scroll(function() {
        if ($(window).scrollTop() > offset.top) {
            sidebar2.stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding2
            });
        } else {
            sidebar2.stop().animate({
                marginTop: 0
            });
        }
    });   
}); 

==更新日期==

感谢所有支持和想法。我已经完成了问题position的问题 这很简单:Update

    <div class="wrapper morediv">
    <div class="adsl"><image alt='scoll1-2tieng' src='http://www.maydoduonghuyet.com.vn/uploads/partners/scoll1_2tieng.gif' width='120' height='300' /></div>
<div class="adsr"><image alt='scoll2-hdsd' src='http://www.maydoduonghuyet.com.vn/uploads/partners/scoll2_hdsd.jpg' width='120' height='300'  /></div>
</div>

和仅限css:

.wrapper{
    margin:0 auto;
    width: 500px;
    height: 1600px;
    position:relative;
    background: #FF0004
}

.adsl{
    position: fixed; margin-left: -135px; bottom: 0
}
.adsr{
    position: fixed; margin-left: 515px; bottom: 0
}

全部谢谢

2 个答案:

答案 0 :(得分:2)

如果您必须始终在底部显示广告

,请使用position:fixed;

demo

.adsl img, .adsr img {
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    position:fixed;
    bottom:0px;
}

.adsl img{
     left:0;  /* set image to left */
     z-index:90; 
}

.adsr img{
     right:0; /* set image to right */
    z-index:90;
}

答案 1 :(得分:1)

如果您希望将广告保留在底部但仍然为其设置动画,请将您的JavaScript更改为

$(document).ready(function() {

    var sidebar   = $(".adsl");
    var offset    = sidebar.offset();
    var topPadding = 10;
    $(window).scroll(function() {
            sidebar.stop().animate({
                marginTop: $(window).scrollTop() + sidebar.height()/2 + topPadding
            });
    });    
    var sidebar2   = $(".adsr");
    var offset2    = sidebar2.offset();
    var topPadding2 = 10;
    $(window).scroll(function() {
            sidebar2.stop().animate({
                marginTop: $(window).scrollTop() + sidebar2.height()/2 + topPadding2
            });
    });

});

Working jsFiddle


旁注:上面的代码段只能用几行编写,但我不确定这种效果是否符合OP的要求。