jQuery - Treat multiple instances of the same class separately?

让人想犯罪 __ 提交于 2020-01-03 17:49:08

问题


Goal:

  1. I'm trying to create a parallax scrolling effect.

  2. The parallax-container are implemented like so: < div class="parallax slide-1" > < /div >

  3. I need the parallax effect to start, when its container is scrolled into view.

  4. Once it has left the view, the effect needs to stop.

The Problem:

The jQuery works fine so far.

But: Since I can have multiple parallax-container on one page (e.g. one at the top + one at the bottom) I need them to be treated independently by jQuery.

At the moment the effect is...

  • 1.) triggered for every parallax-container once the first one is scrolled into view and
  • 2.) stops for every parallax-container once it has left the view.

So not quite the solution yet.

Thoughts

I think it should work with jQuerys .each(), but I couldn't really get it to work so far.

I think I'm getting confused with the nested functions somewhere, when I try to implement it.

Code

Here's my current code:

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

})

回答1:


Assuming the scrolling you want to do is identical (using the same parallax methods,etc), you could just use the .each on the class. Example:

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


来源:https://stackoverflow.com/questions/24293318/jquery-treat-multiple-instances-of-the-same-class-separately

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!