jQuery on window scroll animate background image position

前端 未结 6 2297
小鲜肉
小鲜肉 2020-12-12 18:26

I am trying to achieve a scrolling effect using jQuery. I have a background div set to 100% browser window size with overflow hidden. This has a large background image that

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-12 19:03

    Here you go. Background is set to 10% scroll. You can change the background scroll rate by changing the 10 in the code.

    CSS

    html, body{
        height:100%;
        min-height:100%;
        margin:0;
        padding:0;
    }
    .bg{
        width:100%;
        height:100%;
        background: #fff url(..) no-repeat fixed 0 0;
        overflow:auto;
    }
    
    
    ..

    JavaScript

    $('.bg').scroll(function() {
        var x = $(this).scrollTop();
        $(this).css('background-position', '0% ' + parseInt(-x / 10) + 'px');
    });
    

    Check working example at http://jsfiddle.net/Vbtts/
    Click this link for the full screen example: http://jsfiddle.net/Vbtts/embedded/result/


提交回复
热议问题