jQuery-分别对待同一个类的多个实例? [英] jQuery - Treat multiple instances of the same class separately?

查看:101
本文介绍了jQuery-分别对待同一个类的多个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

问题:

jQuery到目前为止运行良好.

The Problem:

The jQuery works fine so far.

但是:由于我可以在一页上有多个视差容器(例如,顶部有一个+底部有一个),因此我需要对它们进行独立的独立处理 jQuery.

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.

目前效果是...

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

所以还不是解决方案.

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

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.

这是我当前的代码:

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

})

推荐答案

假定要进行的滚动是相同的(使用相同的视差方法等),则可以在类上使用.each.示例:

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

这篇关于jQuery-分别对待同一个类的多个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆