简体   繁体   English

使用jQuery创建视差效果

[英]Creating parallax effect using jQuery

I am trying to create a parallax effect using jQuery. 我正在尝试使用jQuery创建视差效果。 I have 3 boxes on the page and I want to collapse all three div in 1 when user scrolls down and when user scrolls up they should separate again. 我在页面上有3个框,当用户向下滚动时,我想将所有三个div合为1,当用户向上滚动时,它们应该再次分开。 so I used scroll event for that, I am not able to separate them when user scrolls up side. 因此,我为此使用了scroll事件,当用户向上滚动时,我无法将它们分开。 this is my fiddle link and following is my code: Latest Fiddle http://jsfiddle.net/iiison/nkf4Y/3/ JS: 是我的小提琴链接 ,下面是我的代码: 最新的小提琴 http://jsfiddle.net/iiison/nkf4Y/3/ JS:

$(window).scroll(function () {
    var x = $(this).scrollTop();
    if(isVis($('.cnt')[0])){
        var x = $('.lft').offset().left,
        y = $('.rit').offset().left;
        $('.lft').offset({left:($('.lft').offset().left+10)});
        $('.rit').offset({left:($('.rit').offset().left-10)});
        //console.log();
    }
});

I have one more doubt, how to collapse the div at particular position according to different screen width. 我还有一个疑问,如何根据不同的屏幕宽度在特定位置折叠div。 Fiddle 小提琴

Would something like this help? 这样的帮助吗?

http://jsfiddle.net/Ms7gw/ http://jsfiddle.net/Ms7gw/

var lastScrollTop = 0;
var leftBox = $('#left').position();
var rightBox = $('#right').position();

$(window).scroll(function(event){
    var st = $(this).scrollTop();
    $('#right').css({left: rightBox.left + st});
    $('#left').css({left: leftBox.left - st});
    lastScrollTop = st;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM