Jquery自动卷轴陈列室

时间:2017-01-03 17:36:57

标签: javascript jquery html scroll

我尝试建立一个类似于this的网站。为此,我编写了以下代码:

var $window = $(window);
var lastScrollTop = 0;
var shown_elements = 0;
var next_element = 0;
$window.on('scroll', scroll_to_next);
$window.trigger('scroll');

var window_height = $window.height();
var window_top_position = $window.scrollTop();
var window_bottom_position = (window_top_position + window_height);
var scroll = $(window).scrollTop();

function scroll_to_next() {

  shown_elements = Math.ceil($(window).scrollTop() / window_height);
  next_element = shown_elements * window_height;
  if (lastScrollTop < $(window).scrollTop() && lastScrollTop != 0) {
    //$window.scrollTop(next_element);
    $('html, body').animate({
      scrollTop: next_element
    }, 'slow');
    //alert(next_element);
  }
  lastScrollTop = $(window).scrollTop();
}

这是HTML:

<div class="showroom1">
  <div class="showroom_left">
    <img class="fixed" src="mokups/frame.png">
    <img class="screen" src="mokups/screen2.png">
  </div>
  <div class="showroom_right">
    <h3>Hello</h3>
    <h4>description</h4>
  </div>
</div>

<div class="showroom2">
  <div class="showroom_left">
    <img class="screen" src="mokups/screen1.png">
  </div>
  <div class="showroom_right">
    <h3>Hello</h3>
    <h4>description</h4>
  </div>
</div>
<div class="showroom1">
  <div class="showroom_left">
    <img class="fixed" src="mokups/frame.png">
    <img class="screen" src="mokups/screen2.png">
  </div>
  <div class="showroom_right">
    <h3>Hello</h3>
    <h4>description</h4>
  </div>
</div>

此代码第一次运行。并自动滚动到第二个“陈列室”。但是当我尝试第二次滚动时,页面将不会滚动。

我的代码出了什么问题?

3 个答案:

答案 0 :(得分:1)

我创建了一个JSFiddle一段时间用另一个问题来解决同样的问题。 https://jsfiddle.net/8zgsdzy0/

以下是代码:

&#13;
&#13;
var lastScrollPos = 0;
var scrollFired = false;

var textConainersElement = jQuery('.text-container p');
var mainElem = jQuery("[data-main='true']");
var firstElem = jQuery("section:first-child");
var lastElem = jQuery("section:last-child");
var wrapper = jQuery(".wrapper");

jQuery(document).on('DOMMouseScroll mousewheel', function(e) {

  // if the scroll has occrued already then dont fire it again until transition ended
  if (scrollFired == true) {
    jQuery(window).scrollTop(lastScrollPos);
    return false;
  }

  var inviewElem = jQuery("[data-inview='true']");
  var nextElem = inviewElem.next();
  var prevElem = inviewElem.prev();
  var currentTop = parseInt(firstElem.attr('data-top'));



  if (e.originalEvent.detail > 0 || e.originalEvent.wheelDelta < 0) {
    // Scrolling down 
    // if viewed element is last element do nothing
    if (inviewElem.index() >= lastElem.index())
      return false;

    // if main section is inview then scroll through its elements
    if (inviewElem.index() == mainElem.index()) {
      // if the active child is not the last element then process
      var active = jQuery('.text-container .active');
      if (active.index() != textConainersElement.length - 1) {
        jQuery('.text-container .active').removeClass('active').next().addClass('active');

        // Dont scroll further
        return false;
      }
    }

    var top = currentTop + 100;
    firstElem.css("margin-top", "-" + top + "vh").attr("data-top", top);
    nextElem.attr("data-inview", 'true');
    inviewElem.attr("data-inview", 'false');

  } else {
    // Scrolling up 
    // if viewed element is first element do nothing
    if (inviewElem.index() <= firstElem.index())
      return false;

    // if main section is inview then scroll through its elements
    if (inviewElem.index() == mainElem.index()) {
      // if the active child is not the last element then process
      var active = jQuery('.text-container .active');
      if (active.index() != 0) {
        jQuery('.text-container .active').removeClass('active').prev().addClass('active');

        // Dont scroll further
        return false;
      }
    }

    var top = currentTop - 100;
    firstElem.css("margin-top", "-" + top + "vh").attr("data-top", top);
    prevElem.attr("data-inview", 'true');
    inviewElem.attr("data-inview", 'false');
  }

  // Set values to use for next scrolling event
  lastScrollPos = jQuery(window).scrollTop();
  scrollFired = true;

  // reset scrollFired var after transition ended
  firstElem.one('transitionend', function() {
    scrollFired = false;
  });

  //prevent page fom scrolling
  return false;
});
&#13;
body {
  margin: 0;
}
.wrapper {
  display: block;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}
section {
  display: block;
  width: 100%;
  height: 100vh;
  border-bottom: 2px dashed black;
  position: relative;
  transition: all 0.5s;
  background-color: #c4c4c4;
}
section[data-inview="true"] {
  background-color: #929292;
}
.phone-container {
  align-items: center;
  background: #dedede none repeat scroll 0 0;
  border-left: 5px solid black;
  display: flex;
  float: right;
  height: 100vh;
  justify-content: center;
  width: 500px;
}
.phone {
  width: 200px;
  height: 500px;
  background: #A6A6A6 none repeat scroll 0 0;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
}
section.long-scroll {
  height: auto;
}
p {
  margin-top: 80px;
}
p:first-child {
  margin-top: 0px;
}
.text-container {
  float: left;
  width: 200px;
}
.spacer {
  display: block;
  width: 100%;
}
p.active {
  color: #C1E7FF;
}
.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}
.clearfix {
  display: inline-block;
}
/* start commented backslash hack \*/

* html .clearfix {
  height: 1%;
}
.clearfix {
  display: block;
}
/* close commented backslash hack */

.stuck {
  position: fixed;
  top: 0;
}
.fixed {
  position: fixed;
  top: 0;
}
.sticky-wrapper {
  height: auto !important;
}
.text-container {
  padding-left: 40px;
  padding-top: 40px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='wrapper'>
  <section data-inview='true' data-top='0'>
    Scroll-down
  </section>
  <section class="long-scroll clearfix" id="pin" data-main='true'>
    <div class="text-container">
      <p class="active">Text - 1</p>
      <p>Text - 2</p>
      <p>Text - 3</p>
      <p>Text - 4</p>
    </div>

    <div class="phone-container">
      <div class="phone">Slide-1</div>
    </div>
  </section>
  <section id="unhook"></section>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

请将您的函数scroll_to_next()添加到$(window).scroll();

由于您没有捕获$(窗口)提供的滚动事件。所以这是第一次发生。

Example

答案 2 :(得分:0)

我认为问题在于这行代码:

if (lastScrollTop < $(window).scrollTop() && lastScrollTop != 0)

具体地

lastScrollTop != 0

第一次代码运行变量&#34; lastScrollTop&#34;设置为0,因此 if 语句评估为false并且不滚动,但变量&#34; shown_elements&#34;和&#34; next_element&#34;改变了。第二次通过&#34; lastScrollTop&#34;运行代码已经改变但是它没有像你期望的那样工作,因为其他变量已被改变。

解决方案可能是更改if语句并更改&#34; lastScrollTop!= 0&#34;。