js拖动滑块和点击水波纹效果实例代码


Posted in Javascript onOctober 16, 2018

拖动滑块效果:

先看看效果图:

js拖动滑块和点击水波纹效果实例代码

<!doctype html>
<html>
 
 <head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  <title>Document</title>
  <script type="text/javascript" src=""></script>
 
  <style type="text/css">
   input[type="range"] {
    width: 80%;
    background-color: red;
    border-radius: 15px;
    -webkit-appearance: none;
    height: 1px;
    position: relative;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
   }
 
   input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    background-color: green;
    border-radius: 50%;
    height: 30px;
    width: 30px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
    border: none;
    position: relative;
    z-index: 10;
   }
  </style>
  <script type="text/javascript">
   $(function() {
    $(".input_1").change(function() {
     $("p.p1").text($(this).val());
    })
 
    setInterval(function() {
     $("p.p2").text($(".input_2").val());
    }, 0.01)
   })
  </script>
 </head>
 
 <body>
  <p>添加change事件</p>
  <input type="range" step="0.01" min="0" max="5" value="0">
  <p>0</p>
  <p>添加定时器</p>
  <input type="range" step="0.01" min="0" max="5" value="0">
  <p>0</p>
 </body>
 
</html>

js拖动滑块和点击水波纹效果实例代码

<!DOCTYPE html>
<html>
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>鼠标拖动小方块</title>
<style type="text/css">
.lineDiv {
position: relative;
height: 5px;
background: red;
width: 300px;
margin: 50px auto;
}
 
.lineDiv .minDiv {
position: absolute;
top: -5px;
left: 0;
width: 15px;
height: 15px;
background: green;
cursor: pointer
}
 
.lineDiv .minDiv .vals {
position: absolute;
font-size: 20px;
top: -45px;
left: -10px;
width: 35px;
height: 35px;
line-height: 35px;
text-align: center;
background: blue;
}
 
.lineDiv .minDiv .vals:after {
content: "";
width: 0px;
height: 0px;
border-top: 6px solid blue;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-bottom: 6px solid transparent;
display: block;
margin-left: 11px;
}
</style>
</head>
 
<body>
<center>
<h3>用鼠标拖动小方块<span id="msg">0</span>%</h3>
</center>
<div id="lineDiv">
<div id="minDiv">
<div id="vals">0</div>
</div>
</div>
<script>
window.onload = function() {
 
var lineDiv = document.getElementById('lineDiv'); //长线条
var minDiv = document.getElementById('minDiv'); //小方块
var msg = document.getElementById("msg");
var vals = document.getElementById("vals");
var ifBool = false; //判断鼠标是否按下
 
//鼠标按下方块
minDiv.addEventListener("touchstart", function(e) {
e.stopPropagation();
ifBool = true;
console.log("鼠标按下")
});
 
//拖动
window.addEventListener("touchmove", function(e) {
console.log("鼠标拖动")
if(ifBool) {
var x = e.touches[0].pageX || e.touches[0].clientX; //鼠标横坐标var x
var lineDiv_left = getPosition(lineDiv).left; //长线条的横坐标
var minDiv_left = x - lineDiv_left; //小方块相对于父元素(长线条)的left值 
if(minDiv_left >= lineDiv.offsetWidth - 15) {
minDiv_left = lineDiv.offsetWidth - 15;
}
if(minDiv_left < 0) {
minDiv_left = 0;
}
//设置拖动后小方块的left值
minDiv.style.left = minDiv_left + "px";
msg.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100);
vals.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100);
}
});
//鼠标松开
window.addEventListener("touchend", function(e) {
console.log("鼠标弹起")
ifBool = false;
});
//获取元素的绝对位置
function getPosition(node) {
var left = node.offsetLeft; //获取元素相对于其父元素的left值var left
var top = node.offsetTop;
current = node.offsetParent; // 取得元素的offsetParent
  // 一直循环直到根元素

while(current != null) {

left += current.offsetLeft;

top += current.offsetTop;

current = current.offsetParent;

}
return {
"left": left,
"top": top
};
}
}
</script>
</body>
 
</html>

兼容PC端和移动端

<!DOCTYPE html>
<html>
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>鼠标拖动小方块</title>
<style type="text/css">
.lineDiv {
position: relative;
height: 5px;
background: red;
width: 300px;
margin: 50px auto;
}
 
.lineDiv .minDiv {
position: absolute;
top: -5px;
left: 0;
width: 15px;
height: 15px;
background: green;
cursor: pointer
}
 
.lineDiv .minDiv .vals {
position: absolute;
font-size: 20px;
top: -45px;
left: -10px;
width: 35px;
height: 35px;
line-height: 35px;
text-align: center;
background: blue;
}
 
.lineDiv .minDiv .vals:after {
content: "";
width: 0px;
height: 0px;
border-top: 6px solid blue;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-bottom: 6px solid transparent;
display: block;
margin-left: 11px;
}
</style>
</head>
 
<body>
<center>
<h3>用鼠标拖动小方块<span id="msg">0</span>%</h3>
</center>
<div id="lineDiv">
<div id="minDiv">
<div id="vals">0</div>
</div>
</div>
<script>
window.onload = function() {
 
var lineDiv = document.getElementById('lineDiv'); //长线条
var minDiv = document.getElementById('minDiv'); //小方块
var msg = document.getElementById("msg");
var vals = document.getElementById("vals");
var ifBool = false; //判断鼠标是否按下
//事件
var start = function(e) {
e.stopPropagation();
ifBool = true;
console.log("鼠标按下")
}
var move = function(e) {
console.log("鼠标拖动")
if(ifBool) {
if(!e.touches) { //兼容移动端
var x = e.clientX;
} else {  //兼容PC端
var x = e.touches[0].pageX;
}
//var x = e.touches[0].pageX || e.clientX; //鼠标横坐标var x
var lineDiv_left = getPosition(lineDiv).left; //长线条的横坐标
var minDiv_left = x - lineDiv_left; //小方块相对于父元素(长线条)的left值 
if(minDiv_left >= lineDiv.offsetWidth - 15) {
minDiv_left = lineDiv.offsetWidth - 15;
}
if(minDiv_left < 0) {
minDiv_left = 0;
}
//设置拖动后小方块的left值
minDiv.style.left = minDiv_left + "px";
msg.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100);
vals.innerText = parseInt((minDiv_left / (lineDiv.offsetWidth - 15)) * 100);
}
}
var end = function(e) {
console.log("鼠标弹起")
ifBool = false;
}
//鼠标按下方块
minDiv.addEventListener("touchstart", start);
minDiv.addEventListener("mousedown", start);
//拖动
window.addEventListener("touchmove", move);
window.addEventListener("mousemove", move);
//鼠标松开
window.addEventListener("touchend", end);
window.addEventListener("mouseup", end);
//获取元素的绝对位置
function getPosition(node) {
var left = node.offsetLeft; //获取元素相对于其父元素的left值var left
var top = node.offsetTop;
current = node.offsetParent; // 取得元素的offsetParent
  // 一直循环直到根元素

while(current != null) {

left += current.offsetLeft;

top += current.offsetTop;

current = current.offsetParent;

}
return {
"left": left,
"top": top
};
}
}
</script>
</body>
 
</html>

设置滑块的滑动范围

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title>鼠标拖动小方块</title>
<style type="text/css">
.lineDiv {
position: relative;
height: 5px;
background: red;
width: 95%;
margin: 50px auto;
}
 
.lineDiv .minDiv {
position: absolute;
top: -15px;
left: 0;
width: 35px;
height: 35px;
background: green;
cursor: pointer;
transition: all 0s;
}
 
.lineDiv .vals {
z-index: 100;
position: absolute;
top: 0px;
left: 0px;
width: 0px;
height: 5px;
background: blue;
}
</style>
</head>
 
<body>
<center>
<h3>用鼠标拖动小方块<span id="msg">0</span>%</h3>
</center>
<div id="lineDiv">
<div id="vals"></div>
<!-- -->
<div id="minDiv"></div>
<!-- -->
</div>
<script>
window.onload = function() {
 
var lineDiv = document.getElementById('lineDiv'); //长线条
var minDiv = document.getElementById('minDiv'); //小方块
var minVals = document.getElementById('vals'); //左长线
var msg = document.getElementById("msg"); //最上面的信息
var ifBool = false; //判断滑块是否按下
 
var lineDiv_W = getPosition(lineDiv).width; //长线的长度
var lineDiv_L = getPosition(lineDiv).left; //长线距离html的left
 
var minDiv_W = getPosition(minDiv).width; //滑块的长度
var minDiv_L = getPosition(minDiv).left; //滑块距离html的left
 
var Slider_W_MAX = lineDiv_W - minDiv_W; //滑块可以滑动的最大值px,范围是0~Slider_W_MAX
 
var minNum = 0; //最小值
var maxNum = 500; //最大值
 
var startNum = 100; //起始值
var endNum = 400; //结束值
 
var min_Px = Slider_W_MAX / maxNum * startNum; //滑块可以滑动的最小px
var max_Px = Slider_W_MAX / maxNum * endNum; //滑块可以滑动的最大px
 
var minDiv_left=0; //当前滑块的位置
/*
 Slider_W_MAX  1元对应的px?     1
  maxNum    1   1px对应的金额?
*/
 
function initSlider(initPX) { //设置滑块的初始位置
console.log(initPX);
minDiv_left=initPX; //设置滑块的位置
minDiv.style.left = initPX + "px";
minVals.style.width = initPX + "px";
msg.innerText = parseInt(initPX / Slider_W_MAX * 100);
}
 
(function() { //初始化滑块位置
if(startNum >= 0) { //求出startNum对应的px
initSlider(Slider_W_MAX / maxNum * startNum)
}
})()
 
//事件
var start = function(e) {
ifBool = true;
//console.log("鼠标按下")
}
var move = function(e) {
//console.log("鼠标拖动")
if(ifBool) {
var x; //记录滑块距离html的距离left
if(!e.touches) { //兼容PC端
x = e.clientX;
} else { //兼容移动端
x = e.touches[0].pageX;
}
 
minDiv_left = x - lineDiv_L; //小方块相对于父元素(长线条)的left值 
if(minDiv_left >= Slider_W_MAX) {
minDiv_left = Slider_W_MAX;
}
if(minDiv_left < 0) {
minDiv_left = 0;
}
 
//设置拖动后小方块的left值
initSlider(minDiv_left)
 
}
}
var end = function(e) {
if(minDiv_left>max_Px){
initSlider(max_Px);
}
if(minDiv_left<min_Px){
initSlider(min_Px);
}
ifBool = false;
}
//鼠标按下方块
minDiv.addEventListener("touchstart", start);
minDiv.addEventListener("mousedown", start);
//拖动
window.addEventListener("touchmove", move);
window.addEventListener("mousemove", move);
//鼠标松开
window.addEventListener("touchend", end);
window.addEventListener("mouseup", end);
 
//获取元素的绝对位置
function getPosition(node) {
var width = node.offsetWidth; //元素宽度
var height = node.offsetHeight; //元素高度
var left = node.offsetLeft; //获取元素相对于其根元素的left值var left
var top = node.offsetTop; //获取元素相对于其根元素的top值var top
current = node.offsetParent; // 取得元素的offsetParent
 
// 一直循环直到根元素
while(current != null) {

left += current.offsetLeft;

top += current.offsetTop;

current = current.offsetParent;

}
return {
"width": width,
"height": height,
"left": left,
"top": top
};
}
}
</script>
</body>
 
</html>

点击水波纹效果:

js拖动滑块和点击水波纹效果实例代码

<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JS</title>
  <link rel="stylesheet" type="text/css" href="css/reset.css" rel="external nofollow" />
  <script type="text/javascript" src=""></script>
  <style type="text/css" media="screen">
   ul {
    font-size: 0;
    position: relative;
    padding: 0;
    width: 480px;
    margin: 40px auto;
    user-select: none;
   }
    
   li {
    display: inline-block;
    width: 160px;
    height: 60px;
    background: #E95546;
    font-size: 16px;
    text-align: center;
    line-height: 60px;
    color: white;
    text-transform: uppercase;
    position: relative;
    overflow: hidden;
    cursor: pointer;
   }
    
   .slider {
    display: block;
    position: absolute;
    bottom: 0;
    left: 0;
    height: 4px;
    background: #1685D3;
    transition: all 0.5s;
   }
    
   .ripple {
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.4);
    -webkit-transform: scale(0);
    -ms-transform: scale(0);
    transform: scale(0);
    position: absolute;
    opacity: 1;
   }
    
   .rippleEffect {
    -webkit-animation: rippleDrop .4s linear;
    animation: rippleDrop .4s linear;
   }
    
   @-webkit-keyframes rippleDrop {
    100% {
     -webkit-transform: scale(2);
     transform: scale(2);
     opacity: 0;
    }
   }
    
   @keyframes rippleDrop {
    100% {
     -webkit-transform: scale(2);
     transform: scale(2);
     opacity: 0;
    }
   }
  </style>
 
 </head>
 <body>
  <ul>
   <li>项目一</li>
   <li>项目二</li>
   <li>项目三</li>
   <li class="slider"></li>
  </ul>
 </body>
 <script>
  $("ul li").click(function(e) {
 
   if($(this).hasClass('slider')) {
    return;
   }
 
   var whatTab = $(this).index();
 
   var howFar = 160 * whatTab;
 
   $(".slider").css({
    left: howFar + "px"
   });
 
   $(".ripple").remove();
 
   var posX = $(this).offset().left,
    posY = $(this).offset().top,
    buttonWidth = $(this).width(),
    buttonHeight = $(this).height();
   console.log(posX, posY, buttonWidth, buttonHeight)
 
   $(this).append("<span class='ripple'></span>");
 
   if(buttonWidth >= buttonHeight) {
    buttonHeight = buttonWidth;
   } else {
    buttonWidth = buttonHeight;
   }
 
   var x = e.pageX - posX - buttonWidth / 2;
   var y = e.pageY - posY - buttonHeight / 2;
 
   $(".ripple").css({
    width: buttonWidth,
    height: buttonHeight,
    top: y + 'px',
    left: x + 'px'
   }).addClass("rippleEffect");
 
  });
 </script>
 
</html>
Javascript 相关文章推荐
jquery写个checkbox——类似邮箱全选功能
Mar 19 Javascript
javascript打印大全(打印页面设置/打印预览代码)
Mar 29 Javascript
jquery showModelDialog的使用方法示例详解
Nov 19 Javascript
6种javascript显示当前系统时间代码
Dec 01 Javascript
node.js实现端口转发
Apr 14 Javascript
深入理解AngularJS中的ng-bind-html指令和$sce服务
Sep 08 Javascript
微信小程序 欢迎界面开发的实例详解
Nov 30 Javascript
vue2 前端搜索实现示例
Feb 26 Javascript
JavaScript实现微信红包算法及问题解决方法
Apr 26 Javascript
ES6与CommonJS中的模块处理的区别
Jun 13 Javascript
js实现图片上传到服务器和回显
Jan 19 Javascript
深入了解JavaScript词法作用域
Jul 29 Javascript
ajax与jsonp的区别及用法
Oct 16 #Javascript
JS调用安卓手机摄像头扫描二维码
Oct 16 #Javascript
QRCode.js二维码生成并能长按识别
Oct 16 #Javascript
移动端图片上传旋转、压缩问题的方法
Oct 16 #Javascript
JavaScript实现表单注册、表单验证、运算符功能
Oct 15 #Javascript
从零开始搭建vue移动端项目到上线的步骤
Oct 15 #Javascript
jQuery轻量级表单模型验证插件
Oct 15 #jQuery
You might like
老机欣赏|中国60年代精品收音机
2021/03/02 无线电
第二节--PHP5 的对象模型
2006/11/16 PHP
PHP预定义接口――Iterator用法示例
2020/06/05 PHP
js跟随滚动条滚动浮动代码
2009/12/31 Javascript
JS 非图片动态loading效果实现代码
2010/04/09 Javascript
当鼠标移动到图片上时跟随鼠标显示放大的图片效果
2013/06/06 Javascript
使用jQuery Mobile框架开发移动端Web App的入门教程
2016/05/17 Javascript
在web中js实现类似excel的表格控件
2016/09/01 Javascript
值得分享的Bootstrap Table使用教程
2016/11/23 Javascript
Angular-Touch库用法示例
2016/12/22 Javascript
Angular2实现自定义双向绑定属性
2017/03/22 Javascript
使用Node.js实现简易MVC框架的方法
2017/08/07 Javascript
Angular CLI在Angular项目中如何使用scss详解
2018/04/10 Javascript
实例讲解JS中pop使用方法
2019/01/27 Javascript
Vue+elementui 实现复杂表头和动态增加列的二维表格功能
2019/09/23 Javascript
layui动态渲染生成select的option值方法
2019/09/23 Javascript
JS函数参数的传递与同名参数实例分析
2020/03/16 Javascript
[03:05]《我与DAC》之xiao8:DAC与BG
2018/03/27 DOTA
用Python从零实现贝叶斯分类器的机器学习的教程
2015/03/31 Python
Python时间获取及转换知识汇总
2017/01/11 Python
Python基于Flask框架配置依赖包信息的项目迁移部署
2018/03/02 Python
Python3.6笔记之将程序运行结果输出到文件的方法
2018/04/22 Python
python环形单链表的约瑟夫问题详解
2018/09/27 Python
python内置函数sorted()用法深入分析
2019/10/08 Python
python flask中动态URL规则详解
2019/11/22 Python
Python爬虫爬取微信朋友圈
2020/08/06 Python
Boda Skins皮衣官网:奢侈皮夹克,全球配送
2016/12/15 全球购物
Farfetch台湾官网:奢侈品牌时尚购物平台
2019/06/17 全球购物
美国农场商店:Blain’s Farm & Fleet
2020/01/17 全球购物
史上最全面的Java面试题汇总!
2015/02/03 面试题
会计电算化个人求职信范文
2014/01/24 职场文书
小露珠教学反思
2014/04/30 职场文书
2014中学教师节广播稿
2014/09/10 职场文书
解放思想演讲稿
2014/09/11 职场文书
煤矿安全保证书
2015/02/27 职场文书
高中生军训感言
2015/08/01 职场文书