自定义滚轮

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="Author" content=" ">
    <title>Document</title>
    <style type="text/css">
        *{margin: 0;padding: 0;}
        a{text-decoration: none;}
        ul,li{list-style: none;}
        body{font-size: 14px;font-family: "微软雅黑";-webkit-user-select:none;-moz-user-select:none;user-select:none;-o-user-select:none; -ms-user-select:none;}
        #box{width: 700px;height: 500px;background: #FCF3B4;margin: 30px auto;padding: 20px;font-size: 16px;color: #333;position: relative;overflow: hidden;}
        .pbold{font-size: 20px;font-weight: bold;font-family: "Vivaldi";color: #EF41B7}
        #scrollBar{width: 20px;height: 100%;border-radius:10px;background:linear-gradient(#52C5FC,#E78EFD);position: absolute;top: 0;right: 0;display: none;}
        #bar{width: 20px;height: 50px;background: rgba(0,0,0,.5);border-radius:10px;position: absolute;top: 0;right;z-index: 2}
        #b_over{width: 100%;height: 100%;background: transparent;position: absolute;top: 0;right;z-index: 0}
        #content{position: relative;}
    </style>
</head>
<body>
    <script type="text/javascript">
        window.onload = function(){
            var barDom = document.getElementById("bar");
            var boxDom = document.getElementById("box");
            var overDom = document.getElementById("b_over");//遮罩
            var contentDom = document.getElementById("content");
            var scrollDom = document.getElementById("scrollBar");
            var maxH = boxDom.offsetHeight - parseInt(getStyle(barDom,"height"));//barDom.offsetHeight获取不到barDom.offsetHeight==>0  ==>none
            // console.log(maxH);

            var conHeight = contentDom.offsetHeight - boxDom.offsetHeight + contentDom.offsetTop*2;  //padding值
            barDom.onmousedown = function(e){
                var e = e || window.event;
                e.cancelBubble = true;
                var pos = getXY(e);
                // 只需要高度
                var ctop = pos.y - this.offsetTop;
                // var maxH = boxDom.offsetHeight-this.offsetHeight;
                // var cleft = pos.x - this.offsetLeft;
                boxDom.onmouseout = null;//按下时让onmouseout事件为null;
                document.onmousemove = function(e){
                    var e = e || window.event;
                    var pos = getXY(e);
                    var ntop = pos.y - ctop;
                    if(ntop<=0)ntop=0;
                    if(ntop>=maxH)ntop = maxH;
                    barDom.style.top = ntop + "px";
                    contentDom.style.top = -(ntop/maxH)*conHeight + "px";
                };
                document.onmouseup = function(){
                    document.onmousemove = null;
                    document.onmouseup = null;
                    // 放下时再开启事件;
                    boxDom.onmouseout = function(){
                        scrollDom.style.display = "none";
                        if(document.onmousewheel === null){
                            document.onmousewheel = null;
                        }else{
                           document.removeEventListener("DOMMouseScroll",scrollFn);
                        };
                    }
                };

            };

            // overDom.onmousedown = function(e){
            //     var e = e||window.event;
            //     var ctop = getXY(e).y - boxDom.offsetTop - barDom.offsetHeight; //
            //     if(ctop<=0)ctop = 0;
            //     barDom.style.top = ctop + "px";
            //     // console.log(ctop,maxH,conHeight);
            //     contentDom.style.top = -(ctop/maxH)*conHeight + "px";
            // }

            scrollDom.onmousedown = function(e){ //错误 ,同时触发barDom, 在boxDom
                var e = e||window.event;
                var ctop = getXY(e).y - boxDom.offsetTop - barDom.offsetHeight; //
                if(ctop<=0)ctop = 0;
                barDom.style.top = ctop + "px";
                // console.log(ctop,maxH,conHeight);
                contentDom.style.top = -(ctop/maxH)*conHeight + "px";
            };

            boxDom.onmouseover = function(){
                scrollDom.style.display = "block";
                // 兼容
                // alert(document.onmousewheel);
                if(document.onmousewheel === null){
                    document.onmousewheel = scrollFn;
                }else{
                    document.addEventListener("DOMMouseScroll",scrollFn);
                };
            };

            // 滚轮事件
            function scrollFn(e){
                var e = e||window.event;
                var d = e.wheelDelta || -e.detail;
                var ntop = parseInt(getStyle(barDom,"top")); //去单位
                if(d<0){
                    ntop += 20;
                }else{
                    ntop -= 20;
                };
                if(ntop<=0)ntop=0;
                console.log(ntop);
                if(ntop>=maxH)ntop = maxH;
                barDom.style.top = ntop + 'px';
                contentDom.style.top = -(ntop/maxH)*conHeight + "px";
                // 屏蔽浏览器滚轮;
                e.preventDefault();
            };

            boxDom.onmouseout = function(){ //只在这里写,拖动的时候 若移出boxDom.就会触发.
                scrollDom.style.display = "none";
                // 清除滚轮
                if(document.onmousewheel === null){
                    document.onmousewheel = null;
                }else{
                    document.removeEventListener("DOMMouseScroll",scrollFn);
                };
            };


            // 获取鼠标坐标
            function getXY(e){
                var e = e || window.event;
                var x=0,y=0;
                if(e.pageX){
                    x = e.pageX;
                    y = e.pageY;
                }else{
                    var ctop = 0,cleft =0;
                    if(document.documentElement){
                        ctop = document.documentElement.scrollTop;
                        cleft = document.documentElement.scrollLeft;
                    }else{
                        ctop = document.body.scrollTop;
                        cleft = document.body.scrollLeft;
                    }
                    x = ctop + e.clientX;
                    y = cleft + e.clientY;
                };
                return {x:x,y:y};
            };
            function getStyle(obj,attr){//有单位
                return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj)[attr];
            };
        };
    </script>
    <div id="box">
        <div id="scrollBar">
            <div id="bar"></div>
            <div id="b_over"></div>
        </div>

        <div id="content" onselectstart="return false">
            <h2>阿甘正传</h2>
            <br/>
            <p class="pbold">1、Life was like a box of chocolates, you never know what you're going to get.</p>
          <p>生命就像一盒巧克力,结果往往出人意料。</p>
          <p class="pbold">2、Stupid is as stupid does.</p>
          <p>蠢人做蠢事,也可理解为傻人有傻福。</p>
          <p class="pbold">3、Miracles happen every day.</p>
          <p>奇迹每天都在发生。</p>
          <p class="pbold">4、it made me look like a duck in water.</p>
          <p>它让我如鱼得水。</p>
          <p class="pbold">5、I don't know if we each have a destiny, or if we're all just floating around accidental—like on a breeze.</p>
          <p>我不懂我们是否有着各自的命运,还是只是到处随风飘荡。</p>
          <p class="pbold">6、Death is just a part of life, something we're all destined to do.</p>
          <p>死亡是生命的一部分,是我们注定要做的一件事。</p>
          <p class="pbold">7、You have got to put the past behind you before you can move on.</p>
          <p>放下包袱,继续前进。</p>
          <p class="pbold">8、It was like just before the sun goes to bed down on the bayou. There was a million sparkles on the river.</p>
          <p>就像太阳在落山前映射在河口上,有无数的亮点在闪闪发光。</p>
          <p class="pbold">9、If there is anything you need, I will not be far away.</p>
          <p>只要你需要,我就在这里。</p>
          <p class="pbold">10、I am a man of my word.</p>
          <p>我是信守承诺的人。</p>
          <p class="pbold">11、There is one small step for a man,a giant leap for mankind.</p>
          <p>某人的一小步就是人类的一大步。</p>
        </div>
    </div>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android WheelView 是一个 Android 平台上的自定义滚轮选择器控件,可以用于实现日期、时间、地区等各种选择功能。要自定义 WheelView 的样式,可以按照以下步骤进行操作: 1. 首先,在布局文件中将 WheelView 控件引入到需要使用的界面上。 ``` <com.example.WheelView android:id="@+id/wheel_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> ``` 2. 创建一个数值列表或字符串列表,作为数据源,例如: ``` List<String> dataList = new ArrayList<>(); dataList.add("选项1"); dataList.add("选项2"); dataList.add("选项3"); ``` 3. 在代码中获取 WheelView 对象,并设置数据源和样式: ``` WheelView wheelView = findViewById(R.id.wheel_view); wheelView.setDataList(dataList); // 设置数据源 wheelView.setTextSize(20); // 设置文字大小 wheelView.setSelectedTextColor(Color.RED); // 设置选中项文字颜色 wheelView.setUnselectedTextColor(Color.GRAY); // 设置未选中项文字颜色 ``` 4. 若要设置选中项的背景颜色,可以在自定义的 WheelView 类中重写 `setSelection()` 方法: ``` public class WheelView extends View { // ... @Override public void setSelection(int index) { // 设置选中项的背景颜色 } } ``` 5. 如需设置滚轮的分隔线,可以在自定义的 WheelView 类中重写 `onDraw()` 方法: ``` @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制滚轮的分隔线 } ``` 6. 其他样式的设置,如滚轮的背景、滚轮的间距等,也可以在自定义的 WheelView 类中设置。 通过以上步骤,我们可以自定义 Android WheelView 的样式,实现滚轮选择器的个性化显示效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值