Hoang An
Hoang An

Reputation: 23

how to change image top to bottom by jquery

i'm have ads code in jsfiddle: Fiddle

How to change 2 ads to always show bottom when i'm scroll the page (or change Padding / margin bottom)

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
            });
        }
    });   
}); 

==Up date==

Thanks for all suppot and ideas. i'm done questions by ideas position
This is code easy: 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>

and css only:

.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
}

Thanks all

Upvotes: 1

Views: 960

Answers (2)

hex494D49
hex494D49

Reputation: 9235

If you want to keep ads at the bottom but still animate them, change your JavaScript to this

$(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


Side note: The snippet above could be written just in couple of lines but I'm not sure if this effect is what the OP wants.

Upvotes: 1

NoobEditor
NoobEditor

Reputation: 15891

use position:fixed; if you have to always show the ads at the bottom

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;
}

Upvotes: 2

Related Questions