Javascript jQuery:满足条件后更改类

Javascript jQuery:满足条件后更改类,javascript,jquery,html,css,parallax,Javascript,Jquery,Html,Css,Parallax,我在stackoverflow上创建了这个示例,其中有多个滑块垂直移动 加载时,站点有一个溢出:隐藏在主体上,位置固定在我的主要内容div(div class=“content fs row”)。其思想是,当您第一次到达页面时,滚动浏览每个幻灯片,一旦点击最后一张幻灯片,主内容div(div class=“content fs row”)上的位置将从固定变为静态,溢出:hidden将从主体中移除。我在编写“如果它是最后一个滑块,请更改位置”的条件语句时遇到问题。下面的jquery是我在站点上使用

我在stackoverflow上创建了这个示例,其中有多个滑块垂直移动

加载时,站点有一个溢出:隐藏在主体上,位置固定在我的主要内容div
(div class=“content fs row”)
。其思想是,当您第一次到达页面时,滚动浏览每个幻灯片,一旦点击最后一张幻灯片,主内容div
(div class=“content fs row”)
上的位置将从固定变为静态,溢出:hidden将从主体中移除。我在编写“如果它是最后一个滑块,请更改位置”的条件语句时遇到问题。下面的jquery是我在站点上使用的代码以及不起作用的条件语句

如有任何建议,将不胜感激

jquery:

function scrollLax(){
/*
initialize
*/
var scrollDown = false;
var scrollUp = false;
var scroll = 0;
var $view = $('#portfolio');
var t = 0;
var h = $view.height() - 250;


    $view.find('.portfolio-sliders').each(function() {
    var $moving = $(this);
            // position the next moving correctly
        if($moving.hasClass('from-bottom')) {
          $moving.css('top', h); // subtract t so that a portion of the other slider is showing
        } 
    // make sure moving is visible
    $moving.css('z-index', 10);
    });
var $moving = $view.find('.portfolio-sliders:first-child');
        $moving.css('z-index', 10);

/*
event handlers
*/

var mousew = function(e) {
    var d = 0;
    if(!e) e = event;
    if (e.wheelDelta) {
        d = -e.wheelDelta/3;
    } else if (e.detail) {
        d = e.detail/120;
    }
    parallaxScroll(d);
}
    if (window.addEventListener) {
        window.addEventListener('DOMMouseScroll', mousew, false);
    }
    window.onmousewheel = document.onmousewheel = mousew;
/*
parallax loop display loop
*/
window.setInterval(function() {
    if(scrollDown)
    parallaxScroll(4);
    else if(scrollUp)
    parallaxScroll(-4);
}, 50);

function parallaxScroll(scroll) {
    // current moving object
    var ml = $moving.position().left;
    var mt = $moving.position().top;
    var mw = $moving.width();
    var mh = $moving.height();
    // calc velocity
    var fromBottom = false;
    var vLeft = 0;
    var vTop = 0;
    if($moving.hasClass('from-bottom')) {
        vTop = -scroll;
        fromBottom = true;
    }
    // calc new position
    var newLeft = ml + vLeft;
    var newTop = mt + vTop;
    // check bounds
    var finished = false;
    if(fromBottom && (newTop < t || newTop > h)) {
        finished = true;
        newTop = (scroll > 0 ? t : t + h);
    }

    // set new position
    $moving.css('left', newLeft);
    $moving.css('top', newTop);
    // if finished change moving object
    if(finished) {
        // get the next moving
        if(scroll > 0) {
            $moving = $moving.next('.portfolio-sliders');
            if($moving.length == 0)
              $moving = $view.find('.portfolio-sliders:last');
              //this is where I am trying to add the if conditional statement.
                if ('.portfolio-sliders:last')
                  $('.content-fs.row').css({'position': 'static'});
                    if('.portfolio-sliders:last' && (mt == 0))
                      $('html, body').removeClass('overflow');
        } else {
            $moving = $moving.prev('.portfolio-sliders');
            if($moving.length == 0)
              $moving = $view.find('.portfolio-sliders:first-child');
             //reverse the logic and if last slide change position
                if('.portfolio-sliders:first-child')
                 $('.content-fs.row').css({'position': 'fixed'});
          }

    }
        // for debug
    //$('#direction').text(scroll + "/" + t + " " + ml + "/" + mt + " " + finished + " " + $moving.text());

}

}
函数scrollLax(){ /* 初始化 */ var scrollDown=false; var scrollUp=false; var=0; var$view=$(“#投资组合”); var t=0; var h=$view.height()-250; $view.find('.portfolio sliders')。每个(函数(){ var$moving=$(此); //正确定位下一个移动位置 if($moving.hasClass('from-bottom')){ $moving.css('top',h);//减去t,以便显示另一个滑块的一部分 } //确保移动是可见的 $moving.css('z-index',10); }); var$moving=$view.find('.portfolio sliders:first child'); $moving.css('z-index',10); /* 事件处理程序 */ var mousew=函数(e){ var d=0; 如果(!e)e=事件; 如果(e.车轮三角洲){ d=-e.delta/3; }否则,如果(如详细信息){ d=e.详图/120; } 视差克罗尔(d); } if(window.addEventListener){ window.addEventListener('DOMMouseScroll',mousew,false); } window.onmouseheel=document.onmouseheel=mousew; /* 视差环显示环 */ setInterval(函数(){ 如果(向下滚动) 视差croll(4); else if(向上滚动) 视差克罗尔(-4); }, 50); 功能视差滚动(滚动){ //当前移动对象 var ml=$moving.position().left; var mt=$moving.position().top; var mw=$moving.width(); var mh=$moving.height(); //计算速度 var fromBottom=false; var vLeft=0; var-vTop=0; if($moving.hasClass('from-bottom')){ vTop=-滚动; fromBottom=true; } //计算新职位 var newLeft=ml+vLeft; var newTop=mt+vTop; //检查边界 var finished=false; if(从底部开始&(newToph)){ 完成=正确; newTop=(滚动>0?t:t+h); } //重新定位 $moving.css('left',newLeft); $moving.css('top',newTop); //如果完成,请更改移动对象 如果(完成){ //开始下一步 如果(滚动>0){ $moving=$moving.next('.portfolio sliders'); 如果($moving.length==0) $moving=$view.find('.portfolio滑块:last'); //这就是我试图添加if条件语句的地方。 如果('.portfolio滑块:最后一个') $('.content fs.row').css({'position':'static'}); 如果('.portfolio滑块:最后一个'&&(mt==0)) $('html,body').removeClass('overflow'); }否则{ $moving=$moving.prev('.portfolio sliders'); 如果($moving.length==0) $moving=$view.find('.portfolio sliders:first child'); //反转逻辑,如果最后一张幻灯片改变位置 if(“.portfolio滑块:第一个子项”) $('.content fs.row').css({'position':'fixed'}); } } //用于调试 //$(“#方向”).text(滚动+“/”+t+“”+ml+“/”+mt+“”+finished+“”+moving.text()); } }
您的代码只是询问
。公文包滑块:last
是否存在。看来你应该做:

if ($moving == $('.portfolio-sliders:last') )

或者类似的东西,而不是检查活动幻灯片是否是最后一张。

虽然您的网站有点酷,但似乎缺少键盘导航。请务必考虑那些使用替代设备的设备。我有键盘导航,但没有包括。我确实有另一个关于滚动导航的问题,它在firefox上似乎不起作用,但我想这是另一个问题。谢谢你的提示。谢谢你的快速回复,@isherwood!我在添加您建议的代码行(“意外标记:关键字(if)”)时出错。有什么想法吗?我会继续努力的…哦。额外
如果
。已编辑。它将在第一张幻灯片到达顶部后删除类。我也尝试过这个(如果($moving==$('.portfolio sliders:last')),但是我得到了相同的结果。谢谢@isherwood的帮助!