【问题标题】:How do I control the speed of animated scrolling?如何控制动画滚动的速度?
【发布时间】:2017-08-15 11:42:19
【问题描述】:

我目前正在尝试做这样的事情:

$(function() {
  $(".prev").mouseover(function() {
    $("#scrollPane").animate({
      "scrollTop": 0
    }, 1000);
  }).mouseout(function() {
    $("#scrollPane").stop();
  });
  $(".next").mouseover(function() {
    $("#scrollPane").animate({
      "scrollTop": $("#scrollPane .wrap").height()
    }, 1000);
  }).mouseout(function() {
    $("#scrollPane").stop();
  });
});
* {
  margin: 0;
  padding: 0;
  list-style: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  line-height: 1;
}

body {
  margin: 15px auto;
  width: 450px;
}

#scrollPane {
  padding: 10px;
  max-height: 250px;
  overflow: hidden;
  background-color: #fff;
}

#scrollPane p {
  margin: 0 0 10px;
  text-align: justify;
}

.button {
  display: block;
  background-color: #ccf;
  border-radius: 10px;
  cursor: pointer;
  text-align: center;
  font-size: 0.75em;
  padding: 0 0 1px;
}
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<div class="button prev">
  <i class="fa fa-chevron-up"></i>
</div>
<div id="scrollPane">
  <div class="wrap">
    <p>Science is the methodical study of nature including testable explanations and predictions. From classical antiquity through the 19th century, science as a type of knowledge was more closely linked to philosophy than it is now and, in fact, in the
      Western world, the term "natural philosophy" encompassed fields of study that are today associated with science, such as astronomy, medicine, and physics. However, during the Islamic Golden Age foundations for the scientific method were laid by
      Ibn al-Haytham in his Book of Optics. While the classification of the material world by the ancient Indians and Greeks into air, earth, fire and water was more philosophical, medieval Middle Easterns used practical, experimental observation to classify
      materials.</p>
    <p>Today, the ever-evolving term "science" refers to the pursuit of knowledge, not the knowledge itself. It is often synonymous with "natural and physical science" and often restricted to those branches of study relating to the phenomena of the material
      universe and their laws. Although the term implies exclusion of pure mathematics, many university faculties include Mathematics Departments within their Faculty of Science. The dominant sense in ordinary use has a narrower use for the term "science."
      It developed as a part of science becoming a distinct enterprise of defining the "laws of nature"; early examples include Kepler's laws, Galileo's laws, and Newton's laws of motion. In this period it became more common to refer to natural philosophy
      as "natural science." Over the course of the 19th century, the word "science" became increasingly associated with the disciplined study of the natural world, including physics, chemistry, geology and biology. This sometimes left the study of human
      thought and society in a linguistic limbo, which was resolved by classifying these areas of academic study as social science. For example, psychology evolved from philosophy, and has grown into an area of study.</p>
    <p>Currently, there are both "hard" (e.g. biological psychology) and "soft" science (e.g. social psychology) fields within the discipline. As a result, and as is consistent with the unfolding of the study of knowledge and development of methods to establish
      facts, each area of psychology employs a scientific method. Reflecting the evolution of the development of knowledge and established facts and the use of the scientific method, Psychology Departments in universities are found within: Faculty of
      Arts and Science, Faculty of Arts, and a Faculty of Science. Similarly, several other major areas of disciplined study and knowledge exist today under the general rubric of "science", such as formal science and applied science.</p>
  </div>
</div>
<div class="button next">
  <i class="fa fa-chevron-down"></i>
</div>

我正在尝试以更流畅的方式滚动内容。但不幸的是,它不起作用。滚动的速度变得疯狂。当您将鼠标悬停在向上和向下箭头上时,它并不一致。我应该如何控制速度?提前致谢。

【问题讨论】:

  • 对我来说效果很好...很流畅...问题到底出在哪里?
  • 当你悬停时,速度会变得疯狂。向上运动的速度与向下运动的速度不同。如果数据很大,这是非常明显的。比如,五页内容?
  • 好的,我知道该怎么做了。明白了……
  • 你真的应该缓存$("#scrollPane")...大代码优化。 var $scrollPane = $("#scrollPane"),然后重用$scrollPane.

标签: javascript jquery html scroll jquery-animate


【解决方案1】:

我在这方面做了类似的事情。让我更新我原始帖子中的代码:Scroll content on hover using jQuery and the Mathematics behind it,我在其中提到您应该使用一种技术来计算速度以保持一致,而不是时间。现在,滚动的时间是一致的,无论您身在何处,它都会在一秒钟内滚动到相反的方向。

要使其正常工作,您需要考虑容器的scrollTopheight。我建议你这样改变计时功能:

// To top
$("#scrollPane .wrap").outerHeight() - $("#scrollPane .wrap").scrollTop()
// To bottom
$("#scrollPane .wrap").height() * 2 - $(this).scrollTop()

这是上下波动的大致时间。我已将两个时间都从1000 更改为以下代码中的上述表达式。我已经检查过它也可以工作。

$(function () {
  $(".prev").mouseover(function () {
    $("#scrollPane").animate({
      "scrollTop": 0
    }, $("#scrollPane .wrap").outerHeight() - $("#scrollPane .wrap").scrollTop());
  }).mouseout(function () {
    $("#scrollPane").stop();
  });
  $(".next").mouseover(function () {
    $("#scrollPane").animate({
      "scrollTop": $("#scrollPane .wrap").height()
    }, $("#scrollPane .wrap").height() * 2 - $(this).scrollTop());
  }).mouseout(function () {
    $("#scrollPane").stop();
  });
});
* {
  margin: 0;
  padding: 0;
  list-style: none;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  line-height: 1;
}

body {
  margin: 15px auto;
  width: 450px;
}

#scrollPane {
  padding: 10px;
  max-height: 250px;
  overflow: hidden;
  background-color: #fff;
}

#scrollPane p {
  margin: 0 0 10px;
  text-align: justify;
}

.button {
  display: block;
  background-color: #ccf;
  border-radius: 10px;
  cursor: pointer;
  text-align: center;
  font-size: 0.75em;
  padding: 0 0 1px;
}
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<div class="button prev">
  <i class="fa fa-chevron-up"></i>
</div>
<div id="scrollPane">
  <div class="wrap">
    <p>Science is the methodical study of nature including testable explanations and predictions. From classical antiquity through the 19th century, science as a type of knowledge was more closely linked to philosophy than it is now and, in fact, in the
      Western world, the term "natural philosophy" encompassed fields of study that are today associated with science, such as astronomy, medicine, and physics. However, during the Islamic Golden Age foundations for the scientific method were laid by
      Ibn al-Haytham in his Book of Optics. While the classification of the material world by the ancient Indians and Greeks into air, earth, fire and water was more philosophical, medieval Middle Easterns used practical, experimental observation to classify
      materials.</p>
    <p>Today, the ever-evolving term "science" refers to the pursuit of knowledge, not the knowledge itself. It is often synonymous with "natural and physical science" and often restricted to those branches of study relating to the phenomena of the material
      universe and their laws. Although the term implies exclusion of pure mathematics, many university faculties include Mathematics Departments within their Faculty of Science. The dominant sense in ordinary use has a narrower use for the term "science."
      It developed as a part of science becoming a distinct enterprise of defining the "laws of nature"; early examples include Kepler's laws, Galileo's laws, and Newton's laws of motion. In this period it became more common to refer to natural philosophy
      as "natural science." Over the course of the 19th century, the word "science" became increasingly associated with the disciplined study of the natural world, including physics, chemistry, geology and biology. This sometimes left the study of human
      thought and society in a linguistic limbo, which was resolved by classifying these areas of academic study as social science. For example, psychology evolved from philosophy, and has grown into an area of study.</p>
    <p>Currently, there are both "hard" (e.g. biological psychology) and "soft" science (e.g. social psychology) fields within the discipline. As a result, and as is consistent with the unfolding of the study of knowledge and development of methods to establish
      facts, each area of psychology employs a scientific method. Reflecting the evolution of the development of knowledge and established facts and the use of the scientific method, Psychology Departments in universities are found within: Faculty of
      Arts and Science, Faculty of Arts, and a Faculty of Science. Similarly, several other major areas of disciplined study and knowledge exist today under the general rubric of "science", such as formal science and applied science.</p>
  </div>
</div>
<div class="button next">
  <i class="fa fa-chevron-down"></i>
</div>

使用上面的代码,我们通过将内容的滚动速度作为基础而不是时间来做到这一点。在上述情况下,时间将根据scrollPane 元素的scrollTop 值动态计算。我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    $(function() {
      $(".prev").mouseover(function() {
        $("#scrollPane").animate({
          "scrollTop": 0
        }, 10000); // <- this number is animation speed
      }).mouseout(function() {
        $("#scrollPane").stop();
      });
      $(".next").mouseover(function() {
        $("#scrollPane").animate({
          "scrollTop": $("#scrollPane .wrap").height()
        }, 10); // <- this number is animation speed
      }).mouseout(function() {
        $("#scrollPane").stop();
      });
    });
    * {
      margin: 0;
      padding: 0;
      list-style: none;
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
      line-height: 1;
    }
    
    body {
      margin: 15px auto;
      width: 450px;
    }
    
    #scrollPane {
      padding: 10px;
      max-height: 250px;
      overflow: hidden;
      background-color: #fff;
    }
    
    #scrollPane p {
      margin: 0 0 10px;
      text-align: justify;
    }
    
    .button {
      display: block;
      background-color: #ccf;
      border-radius: 10px;
      cursor: pointer;
      text-align: center;
      font-size: 0.75em;
      padding: 0 0 1px;
    }
    <script src="https://code.jquery.com/jquery-2.2.4.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    <div class="button prev">
      <i class="fa fa-chevron-up"></i>
    </div>
    <div id="scrollPane">
      <div class="wrap">
        <p>Science is the methodical study of nature including testable explanations and predictions. From classical antiquity through the 19th century, science as a type of knowledge was more closely linked to philosophy than it is now and, in fact, in the
          Western world, the term "natural philosophy" encompassed fields of study that are today associated with science, such as astronomy, medicine, and physics. However, during the Islamic Golden Age foundations for the scientific method were laid by
          Ibn al-Haytham in his Book of Optics. While the classification of the material world by the ancient Indians and Greeks into air, earth, fire and water was more philosophical, medieval Middle Easterns used practical, experimental observation to classify
          materials.</p>
        <p>Today, the ever-evolving term "science" refers to the pursuit of knowledge, not the knowledge itself. It is often synonymous with "natural and physical science" and often restricted to those branches of study relating to the phenomena of the material
          universe and their laws. Although the term implies exclusion of pure mathematics, many university faculties include Mathematics Departments within their Faculty of Science. The dominant sense in ordinary use has a narrower use for the term "science."
          It developed as a part of science becoming a distinct enterprise of defining the "laws of nature"; early examples include Kepler's laws, Galileo's laws, and Newton's laws of motion. In this period it became more common to refer to natural philosophy
          as "natural science." Over the course of the 19th century, the word "science" became increasingly associated with the disciplined study of the natural world, including physics, chemistry, geology and biology. This sometimes left the study of human
          thought and society in a linguistic limbo, which was resolved by classifying these areas of academic study as social science. For example, psychology evolved from philosophy, and has grown into an area of study.</p>
        <p>Currently, there are both "hard" (e.g. biological psychology) and "soft" science (e.g. social psychology) fields within the discipline. As a result, and as is consistent with the unfolding of the study of knowledge and development of methods to establish
          facts, each area of psychology employs a scientific method. Reflecting the evolution of the development of knowledge and established facts and the use of the scientific method, Psychology Departments in universities are found within: Faculty of
          Arts and Science, Faculty of Arts, and a Faculty of Science. Similarly, several other major areas of disciplined study and knowledge exist today under the general rubric of "science", such as formal science and applied science.</p>
      </div>
    </div>
    <div class="button next">
      <i class="fa fa-chevron-down"></i>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-14
      • 2017-10-23
      • 2019-02-14
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      相关资源
      最近更新 更多