3

I created this site where you have multiple sliders moving vertically using this example on stackoverflow > here < along with this fiddle.

The site when loaded has an overflow: hidden on the body and position fixed on my main content div(div class="content-fs row"). The idea is that when you first arrive on the page, you scroll through each slide and once you hit the last one, the position changes on the main content div(div class="content-fs row") from fixed to static and the overflow: hidden is removed from the body. I'm having trouble writing the conditional statement that says "if its the last slider, change the position." The jquery below is the code i'm using for the site along with the conditional statement that doesn't work.

Any pointers/advice would be greatly appreciated!

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

}

}
Community
  • 1
  • 1
user2647510
  • 189
  • 1
  • 13
  • While your site is kinda cool, it seems to lack keyboard navigation. Please be sure to consider those using alternative devices. – isherwood Feb 14 '14 at 22:04
  • I have the keyboard navigation, but didn't include it. I do have another issue with the scroll navigation, it doesn't seem to work on firefox, but I'm that's for another stackoverflow question. Thanks for the tip though. – user2647510 Feb 14 '14 at 22:26

1 Answers1

1

Your code as it is simply asks whether .portfolio-sliders:last exists. Seems you should be doing:

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

or something along those lines, instead checking whether the active slide is the last.

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Thanks for the speedy response, @isherwood! I get an error when I added the line of code you suggested ("unexpected token: keyword(if)"). Any thoughts? I'll keep trying though... – user2647510 Feb 14 '14 at 22:14
  • Oops. Extra `if`. Edited. – isherwood Feb 14 '14 at 22:14
  • it's removing the classes after the first slide reaches the top. I tried this(if ($moving == $('.portfolio-sliders:last') )) as well, but I get the same result. – user2647510 Feb 14 '14 at 22:25