js拖动滑块和点击水波纹效果实例代码
程序员文章站
2022-06-28 23:46:53
拖动滑块效果:
先看看效果图:
拖动滑块效果:
先看看效果图:
<!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>
<!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>
点击水波纹效果:
<!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>
下一篇: ubuntu下迅雷极速版的安装与使用