回首望,曾经洋洋得意的代码现在不忍直视。曾经看起来碉堡的效果现在也能稍微弄点出来。社会在往前发展,人也得向前迈进。

参考于搜狗浏览器4.2版本首页的上下滚动效果。主要实现整个窗口的上下和左右滚动逻辑,还有很多可以拓展的空间。希望大家能多提意见与建议。

  欢迎fork项目:https://github.com/codetker/myWindowScroll, 实现的效果见http://www.梦萦无双.xyz/myWindowScroll/demo/Default.html, 应用见http://wechat.wutnews.net/Web/PhotoWall/(尚在改善中)。

插件效果:

实现window的上下滚动,默认绑定键盘上下按钮和鼠标滑轮
    实现window的左右滚动,默认绑定键盘左右按钮
    可附加ul li协助控制滚动
    可修改后附加缓动函数,实现多种效果,详情见缓动函数表 http://easings.net/zh-cn#easeInOutQuad

html 结构(ZenCoding形式)

  -div.divClass-div.toLeftOrTop-div.toRightOrBottom-div.stageClass*n-ul.controlUl-li.liClass*n

其中div.toLeftOrTop,div.toRightOrBottom,ul.controlUl可选

调用方法(详情见demo)(按需设置参数)

A.vertical

    $(".divClass").windowScroll({'choose': 0,             //垂直滚动,默认'verticalSpeed': 1,      //控制垂直滚动速度'objControlUl': 'ul.controlUl', //控制垂直滚动按钮,默认为null'list': '.stageClass',          //垂直滚动的对象'crash': true,                  //撞击底部特效'toTop': '.toLeftOrTop',        //向上按钮,默认为null'toBottom': 'toRightOrBottom',  //向下按钮,默认为null'liHover': 'stageSelect'        //设置当前stage的类名});

B.horizontal

    $(".divClass").windowScroll({'choose': 1,               //水平滚动'horizontalSpeed': 1,      //控制水平滚动速度'objControlUl': 'ul.controlUl', //控制水平滚动按钮,默认为null'list': '.stageClass',          //水平滚动的对象'crash': true,                  //撞击左右特效'toTop': '.toLeftOrTop',        //向左按钮,默认为null'toBottom': 'toRightOrBottom',  //向右按钮,默认为null'liHover': 'stageSelect'        //设置当前page的类名});

运行demo
    最简单的方法为改Default.html中jquery对应script元素的src为本地的jquery(离线)或CDN中的jquery(在线),然后双击Default.html即可
    或者配置myBoxScroll.jquery.json or package.json

PS:代码之间耦合过强,可重复利用的很多,准备参考学长的建议按模块写个人函数库,通过模块加载注入需要使用的工具函数

Demo代码如下:

HTML

<!doctype html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta content="" name="keywords" />
<meta content="" name="description" />
<meta name="author" content="codetker" />
<head>
<title>window对象滚动插件</title>
<link href="style/reset.css" rel="stylesheet" type="text/css">
<link href="style/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../src/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../dest/jquery.codetker.windowScroll.min.js"></script>
</head><body scroll="no"><div class="wrap" style="dispaly:block;"><div class="stageControl"><ul><li class="stageSelect">stage1</li><li>stage2</li><li>stage3</li><li>stage4</li><li>stage5</li></ul></div><div class="toTop">Top</div><div class="toBottom">Bottom</div><div class="stage stage1"><div class="pageControl"><ul><li class="pageSelect">page1</li><li>page2</li><li>page3</li></ul></div><div class="toLeft">Left</div><div class="toRight">Right</div><div class="page page1"></div><div class="page page2"></div><div class="page page3"></div>   </div><div class="stage stage2"></div><div class="stage stage3"></div><div class="stage stage4"></div><div class="stage stage5"></div></div>
<script type="text/javascript">$(document).ready(function() {$(".wrap").windowScroll({'choose': 0,'verticalSpeed': 1, 'objControlUl': '.wrap .stageControl','list': '.wrap .stage','crash': true,'toTop': '.toTop','toBottom': '.toBottom','liHover': 'stageSelect'});$(".stage1").windowScroll({'choose': 1,'horizontalSpeed': 1, 'objControlUl': '.stage1 .pageControl','list': '.stage1 .page','crash': true,'toLeft': '.toLeft','toRight': '.toRight','liHover': 'pageSelect'});});
</script>
</body>
</html>

View Code HTML

CSS

@charset "utf-8";
/* CSS Document */
/* whole */body{margin  : 0 0;padding : 0 0;height  : 100%;width   : 100%;overflow: hidden;;}.wrap{font-family: "微软雅黑","宋体", Times, "Times New Roman", serif;font-size  : 14px;margin     : 0 0;padding    : 0 0;height     : 100%;width      : 100%;overflow   : hidden;}
/* whole end *//* stage */.stage,.page{width : 100%;height: 100%;}.stage1{background-color: #b9f579;}.stage2{background-color: #d87efa;}.stage3{background-color: #f97171;}.stage4{background-color: #ffc438;}.stage5{background-color: #9ff4d5;}.stageControl{position: fixed;}.stageControl ul li{-webkit-transition-duration: 0.3s;-o-transition-duration     : 0.3s;transition-duration        : 0.3s;/* 兼容低版本IE,add PIE.htc or IE-css3.htc here */}.stageSelect{background-color: #888 !important;}
/* stage end *//* page */.page{float: left;}.page2{background-color: #fca172;}.page3{background-color: #b6befc;}.pageControl{position: absolute;left    : 200px;}.pageControl ul li{float   : left;-webkit-transition-duration: 0.3s;-o-transition-duration     : 0.3s;transition-duration        : 0.3s;/* 兼容低版本IE,add PIE.htc or IE-css3.htc here */}.pageSelect{background-color: #888 !important;}
/* page end *//* left,right,top,bottom button */.toTop,.toBottom{position: fixed;}.toTop{top :180px;}.toBottom{top :220px;}.toLeft,.toRight{position: absolute;}.toLeft{left: 600px;}.toRight{left: 720px;}.toLeft,.toRight,.toTop,.toBottom,.pageControl ul li,.stageControl ul li{width : 100px;height: 30px;line-height: 30px;text-align : center;cursor: pointer;color : #fff;background-color: #aaa;}.toLeft:hover,.toRight:hover,.toTop:hover,.toBottom:hover,.pageControl ul li:hover,.stageControl ul li:hover{background-color: #888;}
/* left,right,top,bottom button end */

View Code CSS

JavaScript

/** windowScroll 0.1* window滚动插件,上下左右,可选择是否回弹。参考搜狗欢迎页面* 兼容等常见浏览器* 借鉴搜狗4.2版http://ie.sogou.com/features4.2.html*/
;(function($, window, document, undefined) {//定义构造函数var WindowObj = function(ele, opt) {this.$element = ele;this.defaults = {'choose': 0,'list': null,'verticalSpeed': 1,'horizontalSpeed': 1,'objControlUl': null,'toLeft': null,'toRight': null,'toTop': null,'toBottom': null,'crash': true,'liHover': null};this.options = $.extend({}, this.defaults, opt);};//给构造函数添加方法WindowObj.prototype = {//上下滚动的方法verticalMove: function() {varobj = this.$element,speed = this.options.verticalSpeed,objControl = this.options.objControlUl,controlList = $(objControl).find('li'),windowHeight = $(window).height(),list = this.options.list,listMax = $(list).length,toTop = this.options.toTop,toBottom = this.options.toBottom,crashButton = this.options.crash,liHover = this.options.liHover,stop = 0,stageIndex,windowobject = is_chrome();function setCss() {$(obj).css({'width': $(window).width() + 'px','height': $(window).height() * listMax + 'px'});$(list).css({'min-width': $(window).width() + 'px','height': $(window).height() + 'px'});}setCss();function markStage() {getIndex();$(controlList).removeClass(liHover);$(controlList).eq(stageIndex).addClass(liHover);}function is_chrome() {var is_ch = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;if (is_ch) {//判断webkit内核,供scrollTop兼容性使用return 'body';} else {//支持IE和FFreturn 'html';}}//阻止默认行为和冒泡function stopDefaultAndBubble(e) {e = e || window.event;if (e.preventDefault) {e.preventDefault();}e.returnValue = false;if (e.stopPropagation) {e.stopPropagation();}e.cancelBubble = true;}//得到当前的垂直位置function getIndex() {stageIndex = Math.round($(window).scrollTop() / windowHeight);}function goTop() {getIndex();scrollStage(windowobject, stageIndex, 1);}function goBottom() {getIndex();scrollStage(windowobject, stageIndex, -1);}//绑定键盘上下按键事件$(document).keydown(function(event) {/* 绑定keycode38,即向上按钮 */if (event.keyCode == 38) {goTop();} else if (event.keyCode == 40) { //绑定40,即向下按钮
                    goBottom();}});//绑定滑轮功能的函数function handle(delta) {getIndex();if (delta < 0) {scrollStage(windowobject, stageIndex, -1); //stageIndex为当前页码} else {scrollStage(windowobject, stageIndex, 1); //stageIndex为当前页码
                }}//判断滑轮,解决兼容性function wheel(event) {var delta = 0;if (!event) event = window.event;if (event.wheelDelta) {delta = event.wheelDelta;if (window.opera) delta = -delta;} else if (event.detail) {delta = -event.detail;}if (delta)handle(delta); //调用执行函数
            }//注册事件if (window.addEventListener) {window.addEventListener('DOMMouseScroll', wheel, false);}window.onmousewheel = document.onmousewheel = wheel;//绑定鼠标滚轮事件$(document).bind('mousedown', function(e) {if (e.which == 2) { //which=2代表鼠标滚轮,即为中键
                    stopDefaultAndBubble(e);//bugfix 搜狗浏览器的ie内核只有在定时器触发这个函数才生效setTimeout(function() {stopDefaultAndBubble(e);}, 10);}});//如果有ul li控制按钮if (objControl !== null) {$(objControl).delegate('li', 'click', function() {stageIndex = $(this).index();scrollStage(windowobject, stageIndex, 0);});}//如果有上下控制$(toTop).click(function() {goTop();});$(toBottom).click(function() {goBottom();});function scrollStage(obj, stIndex, dir) {varsIndex = stIndex,windowobject = obj,direction = 0 || dir,target = windowHeight * sIndex;function move() {$(windowobject).animate({'scrollTop': target + 'px'}, 1000 * speed, function() {crash(1, target, 20, 150, -1);markStage();});}function after_crash(distant, time, termin) {if (distant <= 15 || time > 150) {stop = 1; //停止碰撞
                        $(windowobject).animate({'scrollTop': termin + 'px'}, time, function() {stop = 0;});}}//撞击函数function crash(direction, termin, distant, time, aspect) {if (crashButton) {if (!stop) {if (direction === 1) {direction = 0;if (aspect === 1) {$(windowobject).animate({'scrollTop': '-=' + distant + 'px'}, time, function() {crash(direction, termin, distant * 0.6, time, 1);after_crash(distant, time, termin);});} else {$(windowobject).animate({'scrollTop': '+=' + distant + 'px'}, time, function() {crash(direction, termin, distant * 0.6, time, -1);after_crash(distant, time, termin);});}} else if (direction === 0) {direction = 1;if (aspect === 1) {$(windowobject).animate({'scrollTop': termin + 'px'}, time, function() {crash(direction, termin, distant * 0.6, time, 1);after_crash(distant, time, termin);});} else {$(windowobject).animate({'scrollTop': termin + 'px'}, time, function() {crash(direction, termin, distant * 0.6, time, -1);after_crash(distant, time, termin);});}}}}}if (!$(windowobject).is(':animated')) {switch (direction) {case 0:if ($(window).scrollTop() > target) {direction = -1;move();} else if ($(window).scrollTop() == windowHeight * sIndex) {direction = 0;crash(1, target, 20, 150, -1);} else {direction = 1;move();}break;case 1:if (sIndex === 0) {crash(1, target, 20, 150, 1);} else {sIndex -= 1;target = windowHeight * sIndex;move();}break;case -1:if (sIndex === listMax - 1) {crash(1, target, 20, 150, -1);} else {sIndex += 1;target = windowHeight * sIndex;move();}break;default:}}}},//左右滚动horizontalMove: function() {varobj = this.$element,speed = this.options.horizontalSpeed,objControl = this.options.objControlUl,controlList = $(objControl).find('li'),windowWidth = $(window).width(),list = this.options.list,listMax = $(list).length,liHover = this.options.liHover,toLeft = this.options.toLeft,toRight = this.options.toRight,crashButton = this.options.crash,pageIndex;function setCss() {$(obj).css({'width': windowWidth * listMax + 'px','height': $(window).height() + 'px'});$(list).css({'width': windowWidth + 'px','min-height': $(window).height() + 'px'});}setCss();function markPage() {getPageIndex();$(controlList).removeClass(liHover);$(controlList).eq(pageIndex).addClass(liHover);}function getPageIndex() {pageIndex = (-1) * Math.round(parseInt($(obj).css('margin-left')) / windowWidth);}function goLeft() {getPageIndex();scrollPage(obj, pageIndex, 1);}function goRight() {getPageIndex();scrollPage(obj, pageIndex, -1);}//绑定键盘左右按键事件$(document).keydown(function(event) {//判断event.keyCode为39(即向右按钮)if (event.keyCode === 39) {goRight();}//判断event.keyCode为37(即向左按钮else if (event.keyCode === 37) {goLeft();}});//如果有ul li控制按钮if (objControl !== null) {$(objControl).delegate('li', 'click', function() {pageIndex = $(this).index();scrollPage(obj, pageIndex, 0);});}//如有有左右控制按钮$(toLeft).click(function() {goLeft();});$(toRight).click(function() {goRight();});function scrollPage(obj, pageIndex, dir) {varwindowobject = obj,direction = 0 || dir,dist = Math.round(parseInt($(obj).css('margin-left'))), aim;function getAim() {aim = pageIndex * windowWidth * (-1);}function crash(type) {if (crashButton) {if (type === 'left') {$(windowobject).animate({'margin-left': '+=' + '50px'}, 500).animate({'margin-left': '-=' + '100px'}, 500).animate({'margin-left': '+=' + '50px'}, 500);} else {$(windowobject).animate({'margin-left': '-=' + '50px'}, 500).animate({'margin-left': '+=' + '100px'}, 500).animate({'margin-left': '-=' + '50px'}, 500);}}}function move() {$(windowobject).animate({'margin-left': aim + 'px' },1000 * speed,function() {markPage();});}if (!$(windowobject).is(':animated')) {switch (direction) {case 0:getAim();if (dist !== aim) {move();} else {direction = 0;crash('left');}break;case 1:if (pageIndex === 0) {crash('left');} else {pageIndex -= 1;getAim();move();}break;case -1:if (pageIndex === (listMax - 1)) {crash('right');} else {pageIndex += 1;getAim();move();}break;default:break;}}}}};//绑定方法到jquery对象原型上$.fn.windowScroll = function(options) {var windowObj = new WindowObj(this, options);if (windowObj.options.choose === 0) {return windowObj.verticalMove();} else if (windowObj.options.choose === 1) {return windowObj.horizontalMove();} else {//add some functions
        }};
})(jQuery, window, document);

View Code JS

转载于:https://www.cnblogs.com/codetker/p/4643815.html

jQuery插件开发之windowScroll相关推荐

  1. vscode插件开发之Swagger生成Ts

    vscode插件开发之Swagger生成Ts 当后端同学给到我们Swagger接口文档的时候,是不是在为要写接口类型烦恼,为了偷懒,那么就any吧.any一时爽,同事泪两行.为了高质量的偷懒,来开发个 ...

  2. android 监听安装来源_Flutter插件开发之APK自动安装

    点击上方的终端研发部,右上角选择"设为星标" 每日早9点半,技术文章准时送上 公众号后台回复"学习",获取作者独家秘制精品资料 往期文章 记五月的一个Andro ...

  3. Jmeter Kafka插件开发之Sampler篇

    Jmeter Kafka插件开发之Sampler篇 背景:在项目中经常需要往kafka里面写数据,并拿生成的数据执行后续的操作,并且有些接口中也需要用到kafka里的数据,小编使用JMeter做接口测 ...

  4. Idea插件开发之Gradle

    Idea插件开发之Gradle 1. 前言 作为Java开发者,Intellij Idea可以说是必备开发工具,Jetbrains提供了丰富的插件库.但是,有时候我们都在做重复.繁琐.无意义的操作,我 ...

  5. CDR插件开发之CPG插件001 - 什么是CPG插件

    CPG,英文缩写 Corel Plugin,中文含义是 Corel 插件,简称CPG插件,典型代表有魔镜.CDR是鼎鼎大名的创意图形设计软件,几乎是广告.印刷行业的标准,而正是由于CDR在日常的排版. ...

  6. CDR插件开发之CPG插件002 - CPG插件开发环境准备和搭建

    上篇文章对什么是CPG插件做了简单的介绍,读者只需要心中有个概念即可.那么,本篇文章就直接开始动手,正式踏上CPG插件开发之路的必经步骤:开发环境搭建.CPG插件不像GMS插件那样,可以在CDR自带的 ...

  7. CDR插件开发之Application(应用程序)

    在CorelDRAW提供的对象模型中,Application是被开发者经常使用的一个对象,也是最顶级的对象.本文对CorelDRAW(以下简称CDR)对象模型中的 Application 类做了一个简 ...

  8. 插件开发之VSCode

    欢迎各位大家关注本人微信公众号:程序员JC,小程序:RobotJC 里面包含很多本人之前做的小案例,也许这里有你想要的东西哦. 鉴于我前两周研究了并开发了一个VSCode插件的情况,这次就讲一下插件开 ...

  9. CDR插件开发之Addon插件005 - Corel.Interop.VGCore.dll库文件简介

    本文讲解了 Corel.Interop.VGCore.dll 库文件的基本概念,演示了如何在CDR软件的安装目录中查找到VGCore库文件所在的位置,最后演示了如何在C#项目中,添加 Corel.In ...

最新文章

  1. webpack源码阅读——npm脚本运行webpack与命令行输入webpack的区别
  2. Spring源码阅读(六)
  3. VS远程开发(远程调试)编译报错:对‘xxx’未定义的引用(设置库依赖顺序)(已解决)pthread(项目-->属性-->链接器-->输入-->库依赖项)
  4. tensorflow环境下的识别食物_研究室秒变后厨,TensorFlow被馋哭!日本团队用深度学习识别炸鸡,救急便当工厂...
  5. [Postman]排除API请求(9)
  6. boot jersey_Jersey和Spring Boot入门
  7. scala bitset_Scala中的BitSet
  8. 二极管的结构、特性、参数、稳压管的特性和参数
  9. 力扣剑指 Offer 17. 打印从1到最大的n位数
  10. 高反差保留滤镜学习OpenCV:滤镜系列(11)——高反差保留
  11. 3D打印设计软件 FreeCAD 入门
  12. 虚拟机Ubuntu21.04全屏显示
  13. 无法完成windows正版认证
  14. springboot2.0启动报错The APR based Apache Tomcat Native library which allows optimal performance in ...
  15. IE的layout布局
  16. python2.7安装pytorch_Pytorch如何安装,Linux安装Pytorch,OSX安装Pytorch教程
  17. Port 1-1023
  18. vue项目yarn初始化项目报错error D:\xxx\node_modules\node-sass;终极解决方案
  19. R 回归分析 逐步回归
  20. 为什么要学网络爬虫?我来告诉你!

热门文章

  1. SQL模糊查询特殊符号应用及详细案例说明
  2. 追影windows7 虚拟4桌面
  3. 机器学习交易——如何使用回归预测股票价格?【翻译】
  4. OS X 10.10.2 也不顶用? Wi-Fi 问题仍存在
  5. UITextField加间隔符号格式化
  6. 在Centos环境下搭建Confluence
  7. 远程计算机已结束连接解决方法
  8. 软启动器说明书_软启动器怎么接线?一张电路图一张实物图供大家参考
  9. java c 转换_java和c通信相关的数据类型转换
  10. vue合并表格excel导出_vue export2Excel 怎么导出一个 excel 同时里面有两个 sheet?