原生JS实现拖拽条效果

效果图

HTML结构:

首先是我们的HTML结构,我们的拖拽条使用了<input />标签来实现,<input type="range" min="0" max="100" step="0.1"value="0" />10类型设置为range即可出现拖拽条样式。
HTML:

    <div id="box">
        <div class="item">
            <p><span>功能</span>-你觉得这个创意在功能上优秀吗?<em>分数越高表示 越优秀</em></p>
            <span>0</span>0<input type="range" min="0" max="100" step="0.1" value="0" />10
        </div>
        <div class="item">
            <p><span>外观</span>-你觉得这个创意在外观上优秀吗?<em>分数越高表示 越优秀</em></p>
            <span>0</span>0<input type="range" min="0" max="100" step="0.1"value="0" />10
        </div>
        <div class="item">
            <p><span>成本</span>-你觉得这个创意在成本上优秀吗?<em>分数越高表示 越优秀</em></p>
            <span>0</span>0<input type="range" min="0" max="100" step="0.1"value="0" />10
        </div>
        <div class="item">
            <p><span>难度</span>-你觉得这个创意在难度上优秀吗?<em>分数越高表示 越优秀</em></p>
            <span>0</span>0<input type="range" min="0" max="100" step="0.1"value="0" />10
        </div>
        <div class="item">
            <p><span>环保</span>-你觉得这个创意在环保上优秀吗?<em>分数越高表示 越优秀</em></p>
            <span>0</span>0<input type="range" min="0" max="100" step="0.1"value="0" />10
        </div>
    </div>

CSS样式:

我们可以单独对拖拽条的按钮和条进行样式的设置,其中background-size: 0 100%;是我们实现拖拽按钮左右颜色不同的关键。

        input[type="range"] {
            /*-webkit-box-shadow: 0 1px 0 0px #424242, 0 1px 0 #060607 inset, 0px 2px 10px 0px black inset, 1px 0px 2px rgba(0, 0, 0, 0.4) inset, 0 0px 1px rgba(0, 0, 0, 0.6) inset;*/
            -webkit-appearance: none;
            outline: none;
            /*去除默认样式*/
            background-color: rgb(137,200,173);
            background: -webkit-linear-gradient(rgb(137,200,173), rgb(137,200,173)) no-repeat, #ddd;
            background-size: 0 100%;
            border-radius: 10px;
            /*border-radius: 15px;*/
            width: 300px !important;
            -webkit-appearance: none;
            height: 12px;
            padding: 0;
            border: none;
            margin-bottom: 10px;
            /*input的长度为80%,margin-left的长度为10%*/
            margin: 0 10px;
        }
        input[type="range"]::-webkit-slider-thumb {
            -webkit-appearance: none;
            /*去除默认样式*/
            cursor: default;
            top: 0;
            height: 30px;
            width: 10px;
            transform: translateY(0px);
            /*background: none repeat scroll 0 0 #5891f5;*/
            background: rgb(137,200,173);
            border-radius: 15px;
            /*-webkit-box-shadow: 0 -1px 1px #fc7701 inset;*/
        }
        #box{
            width: 900px;
            margin: 0 auto;
            overflow: hidden;
            font-size: 14px;
        }
        .item{
            width: 400px;
            margin: 0 12px;
            height: 100px;
            float: left;
        }
        .item p{
            color: rgb(190, 188, 21);
        }
        .item p span{
            font-size: 20px;
        }
        .item p em{
            font-style: normal;
            color: rgb(145,197,69);
        }
        .item>span{
            display: inline-block;
            width: 40px;
            height: 30px;
            border: 1px solid black;
            margin-right: 10px;
            text-align: center;
            line-height: 30px;
        }

JS逻辑:

        var int_ele = document.querySelectorAll("input");
        var box_ele = document.getElementById("box");
        on(box_ele, "input", "input",function(e){
            var target = e.target;
            // 当前数值
            target.previousElementSibling.innerText = Math.floor(target.value * 100) / 1000;
            // 设置进度条颜色
            target.style.backgroundSize = target.value + "% 100%";
        });
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,429评论 0 3
  • CSS基础 本文包括CSS基础知识选择器(重要!!!)继承、特殊性、层叠、重要性CSS格式化排版单位和值盒模型浮动...
    廖少少阅读 3,275评论 0 40
  • 一、HTML5 1.1 认识HTML5 HTML5并不仅仅只是作为HTML标记语言的一个最新版本,更重要的是它制定...
    福尔摩鸡阅读 16,205评论 14 51
  • 一片桃叶 渡我一生 茫茫人海中相遇 是祸是福 我一生漂泊 好在还有你在渡口
    半唐主义阅读 243评论 0 2
  • 70.4符号形状 TikZ宏包shapes.symbols 这个库定义了可以用来绘制禁止符号或云之类符号的形状。 ...
    7300T阅读 187评论 0 2