最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - replace image in div on scroll down - Stack Overflow

programmeradmin8浏览0评论

wanna replace the image in #logo with the image resized.png upon scroll down and on scroll up should return to normal.

tried with the code

<script type="text/javascript">
$(function(){
$(window).scroll(function(){
if($(this).scrollTop() > 100) $('#topbar, .cart-label').fadeOut('slow');
$('#logo').css({'background-image':'url(.png)','background-repeat':'no-repeat'})
if($(this).scrollTop() < 100) $('#logo, #topbar, .cart-label').fadeIn('fast');
})
});

resized.png does e on top but wanna replace it altogether and be restored upon scroll to top. the link to the site is :/ can sum1 suggest ?

wanna replace the image in #logo with the image resized.png upon scroll down and on scroll up should return to normal.

tried with the code

<script type="text/javascript">
$(function(){
$(window).scroll(function(){
if($(this).scrollTop() > 100) $('#topbar, .cart-label').fadeOut('slow');
$('#logo').css({'background-image':'url(http://elementsmart./wp-content/uploads/2013/06/resized.png)','background-repeat':'no-repeat'})
if($(this).scrollTop() < 100) $('#logo, #topbar, .cart-label').fadeIn('fast');
})
});

resized.png does e on top but wanna replace it altogether and be restored upon scroll to top. the link to the site is :http://elementsmart./product/deep-azure-rajasthani-necklace-set/ can sum1 suggest ?

Share Improve this question edited Jun 30, 2013 at 13:04 dhruv asked Jun 30, 2013 at 12:54 dhruvdhruv 1512 gold badges3 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

UPDATE ANSWER

Check here, working DEMO http://jsfiddle/yeyene/49HA3/1/

You are trying to give background-image wrongly to a tag, actually you need to change the src of img tag within a tag.

That is why, you got 2 images. One for a tag background-image, another is img within a tag.

$(function(){
    $(window).scroll(function(){
        if($(this).scrollTop() > 100) {
            $('#topbar, .cart-label').fadeOut('slow');
            $('#logo-img img')
                .css({'width':'184px','height':'33px'})
                .attr('src','http://elementsmart./wp-content/uploads/2013/06/resized.png');
        }
        if($(this).scrollTop() < 100) {
            $('#logo, #topbar, .cart-label').fadeIn('fast');
            $('#logo-img img')
                .css({'width':'184px','height':'60px'})    
                .attr('src','http://elementsmart./wp-content/uploads/2013/06/logo2.png');
        }
    });
});
发布评论

评论列表(0)

  1. 暂无评论