javascript实现菜单滑动(拖动)
最近因为项目需要,写了一个小插件jScroll,就是在固定宽度的容器中实现内容元素的滚动,知道滑动(拖动)的原理后,插件写起来就会非常顺手的。
原理:一个包装元素wrapper,position: relative,一个滑动元素scroller,position:relative|absolute,不过我更倾向于是relative,因absolute在wrapper没在固定高度时会出现内容显示不完全的问题。当scroller滑动(拖动)时,相应地设置scroller的left值,就OK拉。不过兼容性问题还是要花费一些时间解决的。
jScroll插件源码:
(function(){
jScroll = function(el, options){
var that = this;that.wrapper = $(el);that.scroller = that.wrapper.children(":first");
that.wrapper.css({ overflow: "hidden"});
that.options = {};$.extend(that.options, options || {});that._init();}jScroll.prototype = {
newX: 0,min: 0,max: 0,ds: null,
_init: function(){
var that = this;var startX = 0, curX = 0, pos;
var children = that.scroller.children();
var scrollerW = 0;
var start = function(e){e = e || window.event; // 解决IE不兼容问题pos = that.scroller.position();that.min = that.wrapper.width() - that.scroller.width();startX = that._getX(e);that.ds.ontouchmove = that.ds.onmousemove = move;return false;};var move = function(e){e = e || window.event;
curX = that._getX(e);that.newX = (curX - startX) + pos.left;that.scroller.css({ left: that.newX });that.ds.ontouchend = document.onmouseup = end;
return false;};var end = function(e){e = e || window.event;
curX = that._getX(e);that.newX = (curX - startX) + pos.left;if(that.newX > that.max){
that.scroller.animate({ left: that.max}, "slow");
} else if(that.newX < that.min) {that.scroller.animate({ left: that.min}, "slow");
}that.ds.ontouchmove = that.ds.onmousemove = that.ds.ontouchend = document.onmouseup = null;return false;};that.ds = that.scroller.get(0); // 获取dom元素
children.each(function(){
scrollerW += $(this).outerWidth(true);});that.scroller.width(scrollerW);that.ds.onmousedown = that.ds.ontouchstart = start;},_getX: function(e){
return (e && e.changedTouches) ? e.changedTouches[0].clientX : e.clientX;
},// 公开方法
refresh: function(){
var that = this;that._init();},scrollTo: function(x){var that = this;that.newX = (x > that.max) ? that.max : (x < that.min) ? that.min : x;that.scroller.animate({ left: that.newX}, "slow");
},// index从0开始
scrollToElement: function(index){
var that = this;var children = that.scroller.children();
var count = (index < 0) ? 0 : (index >= children.length) ? (children.length - 1) : index;that.newX = 0;for(var i = 0; i < count; i++){that.newX -= children.eq(i).outerWidth(true);}that.scrollTo(that.newX);
},destroy: function(){
var that = this;that.ds.onmousedown = that.ds.ontouchstart = null;
}}window.jScroll = jScroll;
})();
调用方式:
<script type="text/javascript">
(function(){
var jscorll = new jScroll("#wrapper");//jscorll.scrollTo(-100);
jscorll.scrollToElement(0);})();</script>
自测了iphone、android、ie、chrome、firefox,效果还不错。如有不足之处,望指正,谢谢!
PS:没有找到上传附件的按钮,若是哪位需要示例代码的,请把邮箱留下!
---end
作者:清流鱼
出处:http://www.cnblogs.com/qingliuyu/
新浪微博:http://weibo.com/qingliuyu
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗