zoukankan      html  css  js  c++  java
  • 页面中的平滑滚动——smooth-scroll.js的使用

    正常的本页面锚链接跳转的时候跟PPT似的,特别生硬,用户体验非常差。

    这时候我们就可以借助smooth-scroll.js这个插件,来实现本页面的平滑的跳转。

    1首先,导入必须的JS文件

     <script src="js/jquery-1.10.2.js"></script>
     <script src="js/jquery.smooth-scroll.min.js"></script>
     <script src="js/jquery.ba-bbq.js"></script>

    2我们可以这样来调用插件

    $('a').smoothScroll({});

    3可以根据自己的需要指定一个外部容器,那么滚动就是在这个(#container)容器内发生,而不是在页面级别发生了

    $('#container a').smoothScroll();

    4我们可以可以通过下面的方式来排除指定容器的包含元素

    $('#container a').smoothScroll({
         excludeWithin: ['.container2']
    });

    5通过下面的语句来排除满足指定条件的元素

    $('a').smoothScroll({
        exclude: ['.rough','#chunky']
    });

    6调整滑动到哪个位置就停止

    $('.backtotop').smoothScroll({
       offset: -100
    });

    7设定一个滑动开始之前的回调函数

    $('a').smoothScroll({
       beforeScroll: function() {
          alert('ready to go!'); 
        }
    });

    8设定一个滑动结束的回调函数

     $('a').smoothScroll({
      afterScroll: function() {
         alert('we made it!'); 
      }
    });

    你也可以自己配置参数

     $.smoothScroll({
      //滑动到的位置的偏移量
      offset: 0,
      //滑动的方向,可取 'top' 或 'left'
      direction: 'top',
      // 只有当你想重写默认行为的时候才会用到
      scrollTarget: null,
      // 滑动开始前的回调函数。`this` 代表正在被滚动的元素
      beforeScroll: function() {},
      //滑动完成后的回调函数。 `this` 代表触发滑动的元素
      afterScroll: function() {},
      //缓动效果
      easing: 'swing',
      //滑动的速度
      speed: 400,
      // "自动" 加速的系数
      autoCoefficent: 2
    });

    什么?没看懂??没关系!!下边还有一波无脑操作!

    只需要把文件导入后,将下边的代码copy进去就可以了,便可轻松实现页面中的平滑滚动

    <script>
        $(document)
        .on('click', 'a[href*="#"]', function() {
          if ( this.hash ) {
            $.bbq.pushState( '#/' + this.hash.slice(1) );
            return false;
          }
        })
        .ready(function() {
          $(window).bind('hashchange', function(event) {
            var tgt = location.hash.replace(/^#/?/,'');
            if ( document.getElementById(tgt) ) {
              $.smoothScroll({scrollTarget: '#' + tgt});
            }
          });
    
          $(window).trigger('hashchange');
        });
      </script>

    下面就是点击technolog滑动到 id为a1的div区,简单吧!

    <a href="#a1">Technology</a>
    <div id="a1">
    </div>

    smooth-scroll.js的下载地址?网上好多好多,找不到正确的?留言邮箱啊,博主服务一条龙。。。

  • 相关阅读:
    算法导论第十八章 B树
    腾讯2016春招之算法编程解析
    LeetCode:5_Longest Palindromic Substring | 最长的回文子串 | Medium
    搜狗2016校园招聘之算法编程解析
    linux sed在某些字符串的下一行插入内容?sed在下一行插入?
    linux shell搜索某个字符串,然后在后面加上字符串?字符串后面插入字符串?sed字符串后面插入字符串?
    linux环境中,如何使用tar来创建压缩包?解压缩?
    linux环境中,ssh登录报错,Permission denied, please try again.
    linux环境中安装NRPE插件执行远程"本地资源"检查?NRPE安装?
    linux环境安装nagiosgraph将nagios的性能数据绘制成动态图表?
  • 原文地址:https://www.cnblogs.com/cherishli/p/6941251.html
Copyright © 2011-2022 走看看