最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

js拖動滑塊和點(diǎn)擊水波紋效果實(shí)例代碼

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:06:20
文檔

js拖動滑塊和點(diǎn)擊水波紋效果實(shí)例代碼

js拖動滑塊和點(diǎn)擊水波紋效果實(shí)例代碼:拖動滑塊效果: 先看看效果圖: <!doctype html> <html> <head> <meta charset=UTF-8 /> <meta name=viewport content=width=device-width,initial-scale=1,minimum
推薦度:
導(dǎo)讀js拖動滑塊和點(diǎn)擊水波紋效果實(shí)例代碼:拖動滑塊效果: 先看看效果圖: <!doctype html> <html> <head> <meta charset=UTF-8 /> <meta name=viewport content=width=device-width,initial-scale=1,minimum

拖動滑塊效果:

先看看效果圖:

<!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>添加定時(shí)器</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>鼠標(biāo)拖動小方塊</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>用鼠標(biāo)拖動小方塊<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; //判斷鼠標(biāo)是否按下
 
//鼠標(biāo)按下方塊
minDiv.addEventListener("touchstart", function(e) {
e.stopPropagation();
ifBool = true;
console.log("鼠標(biāo)按下")
});
 
//拖動
window.addEventListener("touchmove", function(e) {
console.log("鼠標(biāo)拖動")
if(ifBool) {
var x = e.touches[0].pageX || e.touches[0].clientX; //鼠標(biāo)橫坐標(biāo)var x
var lineDiv_left = getPosition(lineDiv).left; //長線條的橫坐標(biāo)
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;
}
//設(shè)置拖動后小方塊的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);
}
});
//鼠標(biāo)松開
window.addEventListener("touchend", function(e) {
console.log("鼠標(biāo)彈起")
ifBool = false;
});
//獲取元素的絕對位置
function getPosition(node) {
var left = node.offsetLeft; //獲取元素相對于其父元素的left值var left
var top = node.offsetTop;
current = node.offsetParent; // 取得元素的offsetParent
  // 一直循環(huán)直到根元素
  
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>鼠標(biāo)拖動小方塊</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>用鼠標(biāo)拖動小方塊<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; //判斷鼠標(biāo)是否按下
//事件
var start = function(e) {
e.stopPropagation();
ifBool = true;
console.log("鼠標(biāo)按下")
}
var move = function(e) {
console.log("鼠標(biāo)拖動")
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; //鼠標(biāo)橫坐標(biāo)var x
var lineDiv_left = getPosition(lineDiv).left; //長線條的橫坐標(biāo)
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;
}
//設(shè)置拖動后小方塊的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("鼠標(biāo)彈起")
ifBool = false;
}
//鼠標(biāo)按下方塊
minDiv.addEventListener("touchstart", start);
minDiv.addEventListener("mousedown", start);
//拖動
window.addEventListener("touchmove", move);
window.addEventListener("mousemove", move);
//鼠標(biāo)松開
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
  // 一直循環(huán)直到根元素
  
while(current != null) {  
left += current.offsetLeft;  
top += current.offsetTop;  
current = current.offsetParent;  
}
return {
"left": left,
"top": top
};
}
}
</script>
</body>
 
</html>

設(shè)置滑塊的滑動范圍

<!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>鼠標(biāo)拖動小方塊</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>用鼠標(biāo)拖動小方塊<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; //結(jié)束值
 
var min_Px = Slider_W_MAX / maxNum * startNum; //滑塊可以滑動的最小px
var max_Px = Slider_W_MAX / maxNum * endNum; //滑塊可以滑動的最大px
 
var minDiv_left=0; //當(dāng)前滑塊的位置
/*
 Slider_W_MAX 1元對應(yīng)的px? 1
 maxNum 1 1px對應(yīng)的金額?
*/
 
function initSlider(initPX) { //設(shè)置滑塊的初始位置
console.log(initPX);
minDiv_left=initPX; //設(shè)置滑塊的位置
minDiv.style.left = initPX + "px";
minVals.style.width = initPX + "px";
msg.innerText = parseInt(initPX / Slider_W_MAX * 100);
}
 
(function() { //初始化滑塊位置
if(startNum >= 0) { //求出startNum對應(yīng)的px
initSlider(Slider_W_MAX / maxNum * startNum)
}
})()
 
//事件
var start = function(e) {
ifBool = true;
//console.log("鼠標(biāo)按下")
}
var move = function(e) {
//console.log("鼠標(biāo)拖動")
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;
}
 
//設(shè)置拖動后小方塊的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;
}
//鼠標(biāo)按下方塊
minDiv.addEventListener("touchstart", start);
minDiv.addEventListener("mousedown", start);
//拖動
window.addEventListener("touchmove", move);
window.addEventListener("mousemove", move);
//鼠標(biāo)松開
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
 
// 一直循環(huán)直到根元素  
while(current != null) {  
left += current.offsetLeft;  
top += current.offsetTop;  
current = current.offsetParent;  
}
return {
"width": width,
"height": height,
"left": left,
"top": top
};
}
}
</script>
</body>
 
</html>

點(diǎn)擊水波紋效果:

<!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>項(xiàng)目一</li>
 <li>項(xiàng)目二</li>
 <li>項(xiàng)目三</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>

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

js拖動滑塊和點(diǎn)擊水波紋效果實(shí)例代碼

js拖動滑塊和點(diǎn)擊水波紋效果實(shí)例代碼:拖動滑塊效果: 先看看效果圖: <!doctype html> <html> <head> <meta charset=UTF-8 /> <meta name=viewport content=width=device-width,initial-scale=1,minimum
推薦度:
標(biāo)簽: js 代碼 效果
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top