JavaScript实战(原生range和自定义特效)简单实例


Posted in Javascript onAugust 21, 2016

今天我又码了两个特效:一个是用原生input[type=range]的,另一个完全自定义的;下面是完整代码和演示:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
  <style>
    #tip{
      position: absolute;
      top: 30px;
      left: 0;
      right: 0;
      width: 200px;
      height: 160px;
      margin: auto;
      border: 1px solid gray;
      background-color: cornsilk;
    }
    #tip div{
      position: relative;
      width: 100%;
      height: 80px;
      border-bottom: 1px solid gray;
    }
    .out{
      position: relative;
      left: 16%;
      display: inline-block;
      border: 2px solid royalblue;
      margin-top: 20px;
      width: 130px;
      height: 20px;
      background-color: lightgoldenrodyellow;
    }
    .in{
      display: block;
      height: 20px;
      line-height: 20px;
      text-align: right;
      color: white;
      width: 50%;
      background-image: linear-gradient(to right,powderblue 0%,#336699 50%,red 100%);
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }
    input[type="range"] {
      position: relative;
      left: 19%;
      top: 5px;
      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;
      background-color: lightskyblue;
      border-radius: 15px;
      width: 60%;
      -webkit-appearance: none;
      -moz-appearance: none;
      appearance: none;
      height:15px;
    }
    input[type="range"]::-webkit-slider-thumb {
      -webkit-appearance: none;
      -moz-appearance: none;
      appearance: none;
      height: 20px;
      width: 10px;
      background-color: coral;
      border-radius: 15px;
      -webkit-box-shadow: 0 -1px 1px black inset;
      -moz-box-shadow: 0 -1px 1px black inset;
      box-shadow: 0 -1px 1px black inset;
    }
    input[type="range"]:before{
      content: attr(value);
      color: white;
      border-radius: 5px 0 0 5px;
      background-color: lightskyblue;
    }
    input[type="range"]:after{
      content: attr(max);
      color: white;
      border-radius:0 5px 5px 0;
      background-color: lightskyblue;
    }
    .b{
      display: inline-block;
      width: 22px;
      padding: 0;
    }
    #outer2{left: 5px}
    #btn1{
      position: relative;
      left: 5px;
    }
    #btn2{
      position: relative;
      left: 5px;
    }
  </style>
  <script>
    window.onload = function(){
      //原生组件range
      var inner = document.getElementById('inner1');
      var range = document.getElementById('range');
      range.onclick = function(){
          inner.innerHTML = range.value;
          inner.style.width = range.value+'%';
      };
      range.onmousemove = function(){
          inner.innerHTML = range.value;
          inner.style.width = range.value+'%';
      };
      //自定义组件
      var outer2 = document.getElementById('outer2');
      var inner2 = document.getElementById('inner2');
      var btn1 = document.getElementById('btn1');
      var btn2 = document.getElementById('btn2');
      var id,id1;
      var value = parseInt(inner2.innerHTML);
      var a = parseFloat(window.getComputedStyle(outer2,null).width)/100;
      //减---
      btn1.onmousedown = function(){
        id1 = setTimeout(function change(){
              if(value>0) {
                value--;
                inner2.innerHTML = value;
                inner2.style.width = (value) * a + 'px';
                id = setTimeout(function(){
                  clearTimeout(id);
                  change();
                },16.7);
              }else{clearTimeout(id);}
            },500);
      };
      btn1.onmouseup = function(){clearTimeout(id1);clearTimeout(id)};
      btn1.onclick = function(){
        console.log('a:'+a+','+'value:'+value);
        if(value>0){
          value--;
          inner2.innerHTML = value;
          inner2.style.width = (value)*a+'px';
        }
      };
      //加+++
      btn2.onmousedown = function(){
        id1 = setTimeout(function change(){
          if(value<100) {
            value++;
            inner2.innerHTML = value;
            inner2.style.width = value * a + 'px';
            id = setTimeout(function(){
              clearTimeout(id);
              change();
            },16.7);
          }else{clearTimeout(id);}
        },500);
      };
      btn2.onmouseup = function(){clearTimeout(id1);clearTimeout(id)};
      btn2.onclick = function(){
        if(value<100){
          value++;
          inner2.innerHTML = value;
          inner2.style.width = value*a+'px';
        }
      }
    }
  </script>
</head>
<body>
  <form id="tip">
    <div>
      <span id="outer1" class="out">
        <span id="inner1" class="in">50</span>
      </span>
      <input id="range" class="ran" type="range" min="0" max="100" step="1" value="50">
    </div>
    <div id="d2">
      <input id="btn1" class="b" type="button" value="<">
      <span id="outer2" class="out">
        <span id="inner2" class="in">50</span>
      </span>
      <input id="btn2" class="b" type="button" value=">">
    </div>
    按住按钮0.5秒, 会持续变化!
  </form>
</body>
</html>

第一个的实现很简单,就不做解释了,自己看代码;

这里主要介绍第二个实例的实现:

在我们看到一个需求,或者别人的特效时,不急着去看别人的代码,先想想,要是你,该怎么实现?先把思路整理出来

该特效的实现原理:

1. 一个span内嵌套一个span;

•外面的span:只显示宽、高、边框,背景无

•里面的span:高度和外面一样,宽度为默认的50%,先设置好背景颜色为线性渐变

2. 按钮的onclick事件比较简单,点一下,就改变里面的span的宽度和显示数字

3. 当按钮的onmousedown时,启动计时器,等500ms后执行函数change函数,而change函数是一个用setTimeout回调自身的函数,他会没16.7ms回调一次,达到动画效果

难点解析:

1. 这一句 var a = parseFloat(window.getComputedStyle(outer2,null).width)/100;
用来获得初始值,如果你用outer2.style.width
是得不到值得,当然你也可以将a设个固定值,比如这里可以设为
var a = 1.3,
注意IE9以下不支持getComputedStyle方法,
IE的Element对象有currentStyle属性;

2. 这一句
btn1.onmouseup = function(){clearTimeout(id1);
clearTimeout(id)};
很关键,没了它,在onclick触发之前,会先触发onmosedown,在500ms后,开始执行,之后一直执行外层的计时器;
 
3. 其它的都不是难点;
这个实例其实扩展到其它很多应用,比如可以把中间的显示部分替换为文章、图片等等,再把按钮换成自定义的,效果将会很酷的!

如果您觉得我有写的不好的地方,欢迎指出!

以上这篇JavaScript实战(原生range和自定义特效)简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
参考:关于Javascript中实现暂停的几篇文章
Mar 04 Javascript
javascript实现跳转菜单的具体方法
Jul 05 Javascript
浮动的div自适应居中显示的js代码
Dec 23 Javascript
js实现黑色简易的滑动门网页tab选项卡效果
Aug 31 Javascript
js实现表单多按钮提交action的处理方法
Oct 24 Javascript
用jQuery向div中添加Html文本内容的简单实现
Jul 13 Javascript
Vue数据驱动模拟实现1
Jan 11 Javascript
简单的网页广告特效实例
Aug 19 Javascript
vue使用Proxy实现双向绑定的方法示例
Mar 20 Javascript
vue2之简易的pc端短信验证码的问题及处理方法
Jun 03 Javascript
vuex actions异步修改状态的实例详解
Nov 06 Javascript
jQuery实现动态向上滚动
Dec 21 jQuery
原生JS实现-星级评分系统的简单实例
Aug 21 #Javascript
JSONP和批量操作功能的实现方法
Aug 21 #Javascript
全面解析标签页的切换方式
Aug 21 #Javascript
JavaScript第一篇之实现按钮全选、功能
Aug 21 #Javascript
JS实现图片局部放大或缩小的方法
Aug 20 #Javascript
JS获取及验证开始结束日期的方法
Aug 20 #Javascript
jquery弹出框插件jquery.ui.dialog用法分析
Aug 20 #Javascript
You might like
PHP通用检测函数集合
2006/11/25 PHP
PHP基于单例模式实现的mysql类
2016/01/09 PHP
PHP如何将XML转成数组
2016/04/04 PHP
Yii2中DropDownList简单用法示例
2016/07/18 PHP
php reset() 函数指针指向数组中的第一个元素并输出实例代码
2016/11/21 PHP
Javascript操作select方法大全[新增、修改、删除、选中、清空、判断存在等]
2008/09/26 Javascript
jQuery 行级解析读取XML文件(附源码)
2009/10/12 Javascript
javaScript函数中执行C#代码中的函数方法总结
2013/08/07 Javascript
Jquery选择器中使用变量实现动态选择例子
2014/07/25 Javascript
Bootstrap缩略图与警告框学习使用
2017/02/08 Javascript
详解vue-router 2.0 常用基础知识点之router.push()
2017/05/10 Javascript
JavaScript 判断iPhone X Series机型的方法
2019/01/28 Javascript
layer.open的自适应及居中及子页面标题的修改方法
2019/09/05 Javascript
vue 实现单选框设置默认选中值
2019/11/07 Javascript
使用python BeautifulSoup库抓取58手机维修信息
2013/11/21 Python
Python中join和split用法实例
2015/04/14 Python
详解详解Python中writelines()方法的使用
2015/05/25 Python
python导出hive数据表的schema实例代码
2018/01/22 Python
Python的numpy库中将矩阵转换为列表等函数的方法
2018/04/04 Python
Python中循环引用(import)失败的解决方法
2018/04/22 Python
Python爬虫实现全国失信被执行人名单查询功能示例
2018/05/03 Python
实例讲解python中的序列化知识点
2018/10/08 Python
解决pycharm运行时interpreter为空的问题
2018/10/29 Python
python3+PyQt5 实现Rich文本的行编辑方法
2019/06/17 Python
django删除表重建的实现方法
2019/08/28 Python
CSS3 Flexbox中flex-shrink属性的用法示例介绍
2013/12/30 HTML / CSS
英国文胸专家:AmpleBosom.com
2018/02/06 全球购物
瑞典手机壳品牌:Richmond & Finch
2018/04/28 全球购物
应聘美工求职信
2013/11/07 职场文书
2014年行政部工作总结
2014/11/19 职场文书
2015年元旦主持词结束语
2014/12/14 职场文书
检讨书大全
2015/01/27 职场文书
《妈妈别哭,有我在》读后感3篇
2020/01/13 职场文书
关于Python使用turtle库画任意图的问题
2022/04/01 Python
MySQL数据库 安全管理
2022/05/06 MySQL
Golang并发工具Singleflight
2022/05/06 Golang