zeptoJS:如何像jQuery一样,让滚动变得优雅?

利用jQuery的animate() 方法,我们很容易实现滚动条的平滑滚动效果:

$(function() {
    $(‘#top‘).click(
        function (e) {
            $(‘html, body‘).animate({scrollTop: ‘0px‘}, 1000);
            return false;
        }
    );
});

  

在线示例:利用jQuery实现的返回顶部

最近在做基于zeptoJS手机网页开发,遇到类似需求,实践之后发现zeptoJS并不像Jquery一样支持scrollTop()方法,

所以,即使引入使zeptoJS支持The animate()方法的 fx.js 之后,依然无法实现平滑滚动效果。

GOOGLE了一下发现,原来并不是我一人遇到这种问题,Stack Overflow早已有大量相关讨论:

以上讨论给出了很多解决方案,目前我采用了下面这种:

 ZeptoScroll  插件

 GitHub地址:https://github.com/suprMax/ZeptoScroll

 官网地址:http://max.degterev.me/blog/zepto-scrolling

 zepto.scroll.js

/* Author:
    Max Degterev @suprMax
*/

;(function($) {
  var DEFAULTS = {
    endY: $.os.android ? 1 : 0,
    duration: 200,
    updateRate: 15
  };

  var interpolate = function (source, target, shift) {
    return (source + (target - source) * shift);
  };

  var easing = function (pos) {
    return (-Math.cos(pos * Math.PI) / 2) + .5;
  };

  var scroll = function(settings) {
    var options = $.extend({}, DEFAULTS, settings);

    if (options.duration === 0) {
      window.scrollTo(0, options.endY);
      if (typeof options.callback === ‘function‘) options.callback();
      return;
    }

    var startY = window.pageYOffset,
        startT = Date.now(),
        finishT = startT + options.duration;

    var animate = function() {
      var now = Date.now(),
          shift = (now > finishT) ? 1 : (now - startT) / options.duration;

      window.scrollTo(0, interpolate(startY, options.endY, easing(shift)));

      if (now < finishT) {
        setTimeout(animate, options.updateRate);
      }
      else {
        if (typeof options.callback === ‘function‘) options.callback();
      }
    };

    animate();
  };

  var scrollNode = function(settings) {
    var options = $.extend({}, DEFAULTS, settings);

    if (options.duration === 0) {
      this.scrollTop = options.endY;
      if (typeof options.callback === ‘function‘) options.callback();
      return;
    }

    var startY = this.scrollTop,
        startT = Date.now(),
        finishT = startT + options.duration,
        _this = this;

    var animate = function() {
      var now = Date.now(),
          shift = (now > finishT) ? 1 : (now - startT) / options.duration;

      _this.scrollTop = interpolate(startY, options.endY, easing(shift));

      if (now < finishT) {
        setTimeout(animate, options.updateRate);
      }
      else {
        if (typeof options.callback === ‘function‘) options.callback();
      }
    };

    animate();
  };

  $.scrollTo = scroll;

  $.fn.scrollTo = function() {
    if (this.length) {
      var args = arguments;
      this.forEach(function(elem, index) {
        scrollNode.apply(elem, args);
      });
    }
  };
}(Zepto));

  

DEMO:

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

    <title>ZeptoScroll</title>
    <style type="text/css">

    #main{
        font: normal 13px/1.4em Helvetica, Arial, sans-serif;
        width: 96%;
        padding: 2%;
        max-width: 320px;
        margin: 0 auto;
        background-color: #f5f5f5;
        color:#666;
    }

    #top {
      display: inline-block;
      padding: 6px 0;
      color: #fff;
      width: 100%;
      cursor: pointer;
      text-align: center;
      border-radius: 3px;
      background-color: #00bb9c;
      text-decoration: none;
    }
    </style>
</head>
<body>
    <div id="main">
	<h1>Add some gold.</h1>
	<p>Look what I‘m dealing with, man. I‘m dealing with fools and trolls. I‘m dealing with soft targets, and it‘s just strafing runs in my underwear before my first cup of coffee … they lay down with their ugly wives and their ugly children and just look at their loser lives and then they look at me and say, ‘I can‘t process it.‘ Well, no, and you never will! Stop trying! Just sit back and enjoy the show.</p>

	<p>I am on a drug. It‘s called Charlie Sheen. It‘s not available. If you try it once, you will die. Your face will melt off and your children will weep over your exploded body.</p>

	<p>It‘s being directed and written by a genius named David Ward who, I don‘t know, won the Academy Award at 23 for writing The Sting? It was his pen and his vision that created the classic that we know today as Major League. In fact, a lot of people think the movie‘s called Wild Thing, as they should. Whatever … If they want me in it, it‘s a smash. If they don‘t, it‘s a turd that opens on a tugboat.</p>

	<p>I tried marriage. I‘m 0 for 3 with the marriage thing. So, being a ballplayer — I believe in numbers. I‘m not going 0 for 4. I‘m not wearing a golden sombrero.</p>
        <p>Look what I‘m dealing with, man. I‘m dealing with fools and trolls. I‘m dealing with soft targets, and it‘s just strafing runs in my underwear before my first cup of coffee … they lay down with their ugly wives and their ugly children and just look at their loser lives and then they look at me and say, ‘I can‘t process it.‘ Well, no, and you never will! Stop trying! Just sit back and enjoy the show.</p>

        <p>I am on a drug. It‘s called Charlie Sheen. It‘s not available. If you try it once, you will die. Your face will melt off and your children will weep over your exploded body.</p>

        <a id="top" href="javascript:void(0);">To the top, please!</a>
    </div>

    <script src="http://libs.baidu.com/zepto/0.8/zepto.min.js"></script>
    <script src="static/zepto.scroll.js"></script>
    <script type="text/javascript">
    $(‘#top‘).on(‘click‘, function(e) {
      $.scrollTo({
        endY: 0,
        duration: 200,
        callback: function() {
          alert(‘at the top‘);
        }
      });
    });
    </script>
</body>
</html>

  

时间: 2024-10-23 07:32:31

zeptoJS:如何像jQuery一样,让滚动变得优雅?的相关文章

jQuery 随滚动条滚动效果 (适用于内容页长文章)

直接入题! 当内容页比较长的时候,网站右侧一直是空白,不如放点有用的东西给用户看,最好不要放广告,因为那样很邪恶,你懂的. 好吧,昨天写了这个东西,jQuery滚动随动区块,代码如下: //侧栏随动 var rollStart = $('.feed-mail'), //滚动到此区块的时候开始随动 rollOut = $('.cookie-list'); //隐藏rollStart之后的区块 rollStart.before('<div class="da_rollbox">

jQuery 随滚动条滚动效果 (固定版)

//侧栏随动 var rollStart = $('.feed-mail'), //滚动到此区块的时候开始随动 rollSet = $('.search,.weibo,.group,.feed-mail,.tags'); //添加rollStart之前的随动区块 rollStart.before('<div class="da_rollbox" style="position:fixed;background-color:#fff;width:inherit;"

jQuery Validation让验证变得如此容易(二)

上一个例子我们是统一引用jquery.validate.js这样所有必填字段的提示信息都将是This field is required. 现在要改成动态提示,比如姓名如果为空则提示姓名不能为空,密码如果为空则提示密码不能为空. 这次我们将校验规则写在代码里 首先还是先引入文件 <script src="js/jquery-1.8.0.min.js" type="text/javascript"></script> <script sr

jquery文字垂直滚动代码实例

jquery文字垂直滚动代码实例: 文字垂直滚动效果在大量网站都有应用,尤其在新闻公告或者新闻列表之类形式的功能结构中出现. 代码实例如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.51texiao.cn/" /> <title>蚂

jQuery页面的滚动位置scrollTop、scrollLeft

Web页面常常比显示该页面的浏览器窗口还要大,因为Web文档具有很多内容,往往会导致页面比浏览器还要高,有时候甚至还要宽,这迫使访问者通过滚动来查看整个页面(如图10-8所示).当访问者滚动页面的时候,一部分文档会从视线中消失.例如,Web页面不能完全放入浏览器窗口中,文档会向左或向上滚动,因此,页面的顶部和左边都会消失在视野之内.这意味着浏览器窗口的左上角和文档的左上角并不相同.如果试图放置一个新元素,例如,屏幕顶部的一个动态Banner:而如果只是试图将元素的left和top位置设置为0,将

基于jquery的鼠标滚动放大缩小图片

一直以来都想写一个图片放大和缩小的小玩意,本来以为会很复杂,这几天自己思考了一下,原来是so easy啊.目前实现的放大缩小,主要时依据鼠标的滚轮触发事件来实现的,废话少说直接上源码. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns

js原生 + jQuery实现页面滚动字幕

js原生/jQuery实现页面滚动字幕效果 17:45:49 在新闻列表或者文章列表信息等页面中很容易要求实现字幕滚动的效果,以下为简单的实现页面中滚动字幕的效果 1.jQuery实现页面滚动字幕效果 代码如下: <div class="box"> <ul class="list"> <li>这是滚动加载的第1条数据</li> <li>你猜猜这是第几条滚动加载的文字</li> <li>

jquery图片无缝滚动特效

jquery图片无缝滚动插件制作左右无缝滚动图片和上下无缝滚动图片,一款简单的jQuery无缝滚动代码.JS代码 <script type="text/javascript"> //图片滚动 调用方法 imgscroll({speed: 30,amount: 1,dir: "up"}); $.fn.imgscroll = function(o){ var defaults = { speed: 40, amount: 0, width: 1, dir:

基于jQuery左侧小图滚动右侧大图显示代码

今天给大家分享一款 jQuery左侧小图滚动右侧大图显示代码是一款基于jQuery实现的左侧滚动图片点击大图查看效果代码.该实例适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class="caseImg03 w1002"> <div class="slideCase03"> <div class=&q