首页 > web前端 > js教程 > 正文

js写一个简单的滚动条实例代码

零下一度
发布: 2017-06-27 10:36:07
原创
1201人浏览过
<!DOCTYPE html><html><head><title>滑动条</title><meta charset="utf-8"><meta name="apple-mobile-web-app-capable" content="yes" /><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /><script type="text/javascript" src="./hScoll.js?1.1.11"></script></head><style>*{margin: 0;padding: 0;}#content{margin-top: 50px;width:100%;height: 200px;background: #eeeeee;overflow: hidden;position: relative;/**transform: translate(0px, -70px);*/}#scoll{overflow: hidden;}#content2{margin-top: 50px;width:100%;height: 200px;background: red;overflow: hidden;position: relative;/**transform: translate(0px, -70px);*/}#scoll2{overflow: hidden;}.scrollbars{position: absolute;height: 100%;right: 0;top: 0;width: 5px;border-radius: 5px;}.scollb{position: absolute;right: 0;top: 0;width: 100%;background: #999999;border-radius: 5px;}</style><body><div id="content"><div id="scoll"><p>1111</p><p>2222</p><p>3333</p><p>4444</p><p>5555</p><p>6666</p><p>7777</p><p>8888</p><p>9999</p><p>0000</p><p>aaaa</p><p>bbbb</p><p>cccc</p><p>dddd</p><p>eeee</p></div></div></body><script>var options ={
        interactiveScrollbars:true}
    window.hScoll.buildScoll('content',options);</script></html>
登录后复制

js 代码

/**
 * Created by hechao on 2017/6/25. */(function(){/**添加window对象hScoll属性*/window.hScoll = {


        buildScoll:function(el,options){
            App.init(el,options);
        }
    }var App = {/**初始化组件*/init:function(el,option){
            App.options = option;
            App.prevY = 0;
            App.el = document.getElementById(el);
            App.scoll = this.el.children[0];
            App.h = this.el.offsetHeight;//滑动范围高度App.ch = this.el.scrollHeight;//内容的高度if(parseFloat(this.h)<=parseFloat(this.ch)){
                App.sdiv = document.createElement('div');
                App.scollb = document.createElement('div');
                App.sdiv.setAttribute('class','scrollbars');
                App.scollb.setAttribute('class','scollb');
                App.scollb.style.height = parseFloat(this.h)*parseFloat(this.h)/parseFloat(this.ch) + 'px';
                App.el.appendChild(this.sdiv);
                App.sdiv.appendChild(this.scollb);
                App.initevent();
            }
        },/**绑定事件*/initevent:function (){
            App.el.addEventListener('touchstart', App.touchstart, false);
            App.el.addEventListener('touchmove', App.touchmove, false);
            App.el.addEventListener('touchend', App.touchend, false);
        },/**记录滑动初始位置*/touchstart:function(e){var point = App.getPoint(e);
            App.startY = point.pageY;
        },/**手指移动时,滚动条滚动*/touchmove:function(e){
            e.preventDefault();//阻止默认行为var point = App.getPoint(e);
            App.moveY = point.pageY;
            App.deltaY = App.startY - App.moveY;if((App.prevY - App.deltaY)<=0 && (App.prevY - App.deltaY)>= -(App.ch-App.h)){
                App.domove(App.prevY - App.deltaY);
            }if(App.options.interactiveScrollbars){
                App.domove2(App.prevY - App.deltaY);
            }else{if((App.prevY - App.deltaY)<=0 && (App.prevY - App.deltaY)>= -(App.ch-App.h)){
                    App.domove2(App.prevY - App.deltaY);
                }
            }
        },/**手指离开时,判断位置*/touchend:function(e){
            App.prevY = App.prevY - App.deltaY;if(App.prevY >= 0){
                App.prevY = 0;
                App.domove(App.prevY,true);
                App.domove2(App.prevY,true);
            }if(App.prevY <= -(App.ch-App.h)){
                App.prevY = -(App.ch-App.h);
                App.domove(App.prevY,true);
                App.domove2(App.prevY,true);
            }
        },

        getPoint:function (e) {return e.touches ? e.touches[0] : e;
        },/**内容滑动*/domove:function (y,t){if(t){
                App.scoll.setAttribute('style', 'transform: translate(0px, '+y+'px);transition:transform 300ms ease');
            }else{
                App.scoll.setAttribute('style', 'transform: translate(0px, '+y+'px);transition:transform 0ms ease');
            }
        },/**滚动条滑动*/domove2:function(y,t){if(t){
                App.scollb.setAttribute('style', 'transform: translate(0px, '+-parseFloat(y)*parseFloat(App.h)/parseFloat(App.ch)+'px);transition:transform 0ms ease;height:'+parseFloat(App.h)*parseFloat(App.h)/parseFloat(App.ch) + 'px'+'');
            }else{
                App.scollb.setAttribute('style', 'transform: translate(0px, '+-parseFloat(y)*parseFloat(App.h)/parseFloat(App.ch)+'px);transition:transform 0ms ease;height:'+parseFloat(App.h)*parseFloat(App.h)/parseFloat(App.ch) + 'px'+'');
            }
        }
    }
})();
登录后复制

 

代码小浣熊
代码小浣熊

代码小浣熊是基于商汤大语言模型的软件智能研发助手,覆盖软件需求分析、架构设计、代码编写、软件测试等环节

代码小浣熊 51
查看详情 代码小浣熊

以上就是js写一个简单的滚动条实例代码的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号