【问题标题】:jQuery - Treat multiple instances of the same class separately?jQuery - 分别处理同一类的多个实例?
【发布时间】:2014-06-18 19:09:07
【问题描述】:

目标:

  1. 我正在尝试创建视差滚动效果。

  2. 视差容器的实现方式如下: < div class="parallax slide-1" > < /div >

  3. 当容器滚动到视图中时,我需要启动视差效果。

  4. 一旦它离开视图,效果需要停止。

问题:

到目前为止,jQuery 运行良好。

但是:由于我可以在一个页面上拥有多个视差容器(例如,顶部一个 + 底部一个),我需要通过 独立 处理它们jQuery.

目前效果是……

  • 1.) 一旦第一个视差容器滚动到视图中,就会为每个视差容器触发,并且
  • 2.) 在每个视差容器离开视图后停止。

所以还不是完全的解决方案。

想法

我认为它应该与 jQuerys .each() 一起使用,但到目前为止我还不能真正让它工作。

当我尝试实现它时,我想我对某处的嵌套函数感到困惑。

代码

这是我当前的代码:

$(document).ready(function(){

$.fn.is_on_screen = function(){
    var win = $(window);
    var viewport = {
        top : win.scrollTop(),
        left : win.scrollLeft()
    };
    viewport.right = viewport.left + win.width();
    viewport.bottom = viewport.top + win.height();

    var bounds = this.offset();
    bounds.right = bounds.left + this.outerWidth();
    bounds.bottom = bounds.top + this.outerHeight();

    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
};

$(window).scroll(function(){ // Bind window scroll event
    if( $('.parallax').length > 0 ) { // Check if target element exists in DOM
        if( $('.parallax').is_on_screen() ) { // Check if target element is visible on screen after DOM loaded

            // ANIMATE PARALLAX EFFECT
            // If Parallax Element is scrolled into view do...

            // Variables
                var speed     = 2.5;
                var calc      = (-window.pageXOffset / speed) + "px " + (-window.pageYOffset / speed) + "px";
                var container = $(".parallax");

            // Function
                container.css({backgroundPosition: calc});

        } else {
            // ...otherwise do nothing
        }
    }
});

})

【问题讨论】:

    标签: jquery scroll window parallax


    【解决方案1】:

    假设你想要做的滚动是相同的(使用相同的视差方法等),你可以在类上使用 .each 。示例:

    $(window).scroll(function(){ // Bind window scroll event
        $( ".parallax" ).each(function() {
            if( $( this ).is_on_screen() ) { // Check if target element is visible on screen after DOM loaded
    
                // ANIMATE PARALLAX EFFECT
                // If Parallax Element is scrolled into view do...
                // remember to replace ('.paralax') with (this)
    
                // Variables
                    var speed     = 2.5;
                    var calc      = (-window.pageXOffset / speed) + "px " + (-window.pageYOffset / speed) + "px";
                    var container = $( this );
    
                // Function
                    container.css({backgroundPosition: calc});
    
            } else {
                // ...otherwise do nothing
            }
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 1970-01-01
      • 2014-01-20
      • 2015-05-24
      • 1970-01-01
      • 2013-05-26
      相关资源
      最近更新 更多