javascript原生实现滑块拖拽验证(纯原生)

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;

}

#d1 {
width: 300px;
height: 40px;
border: 1px solid #ccc;
margin-left: 300px;
margin-top: 100px;
position: relative;
display: flex;
flex-direction: row;
flex: 1;
cursor: pointer;

}

#d3 {
flex: 0;
background: #58bc58;
float: left;

}

#d4 {
flex: 1;
float: left;
}

#d2 {
flex: 1;
height: 40px;
width: 40px;
background: orangered;
position: absolute;
top: 0;
left: 0;
float: left;
box-sizing: border-box;
padding: 10px;
cursor: pointer;
user-select: none;
}

#but {
width: 80px;
height: 40px;
position: absolute;
left: 603px;
top: 100px;
cursor: pointer;
font-size: 16px;
}
</style>
<script>
window.onload = function () {
//获取节点
var d1 = document.getElementById("d1");
var d2 = document.getElementById("d2");

//鼠标按下事件
d2.onmousedown = function (ev) {
var ix = ev.offsetX;//获取水平偏移量
document.onmousemove = function (ev) {
var l = ev.clientX - ix - d1.offsetLeft;//水平移动的距离
// console.log(ev.clientX);
if (l <= 0) {//边界限制
l = 0;
d2.style.background = "orangered";//滑块颜色

} else if (l > (d1.offsetWidth - d2.offsetWidth)) {//限制最远距离
l = d1.offsetWidth - d2.offsetWidth;
d2.style.background = "green";滑块颜色
d2.innerHTML = "✔";
// d3.style.display = "block";
}
var x = l / d1.offsetWidth;
d3.style.flex = (x + 0.05);
d4.style.flex = (1 - x);
// if (l = d1.offsetWidth - d2.offsetWidth) {
// d3.style.flex = 1;
// }
d2.style.left = l + "px";//设置左移位置
// d3.style.left = l + "px";
// d1.style.background = "orange";
ev.stopPropagation();
}
document.onmouseup = function (ev) {//鼠标松开
document.onmousemove = null;
var l = d2.offsetLeft//水平移动的距离
if (l == d1.offsetWidth - d2.offsetWidth) {
d2.style.left = (d1.offsetWidth - d2.offsetWidth) + "px";
d2.style.background = "green";滑块颜色
d2.innerHTML = "✔";
d3.style.flex = 1;
d3.style.background = "#58bc58";
d4.style.flex = 0;
// console.log(l);
} else {
d2.style.left = 0;
d2.innerHTML = "→";
d3.style.flex = 0;
d4.style.flex = 1;
}
// ev.stopPropagation();
}

}
but.onclick = function () {
d2.style.left = 0;
d2.innerHTML = "→";
d3.style.flex = 0;
d4.style.flex = 1;
d2.style.background = "orangered";

}

}
</script>

</head>

<body>
<div id="d1">
<div id="d3"></div>

<div id="d2"> → </div>
<div id="d4"></div>

</div>
<input type="button" value="重置" id="but">

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的JavaScript原生选项卡实现HTML部分: ```html <div class="tab"> <button class="tablinks" onclick="openTab(event, 'tab1')">Tab 1</button> <button class="tablinks" onclick="openTab(event, 'tab2')">Tab 2</button> <button class="tablinks" onclick="openTab(event, 'tab3')">Tab 3</button> <div id="tab1" class="tabcontent"> <h3>Tab 1 Content</h3> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> </div> <div id="tab2" class="tabcontent"> <h3>Tab 2 Content</h3> <p>Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> <div id="tab3" class="tabcontent"> <h3>Tab 3 Content</h3> <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris.</p> </div> </div> ``` CSS部分: ```css .tab { overflow: hidden; border: 1px solid #ccc; background-color: #f1f1f1; } .tab button { background-color: inherit; float: left; border: none; outline: none; cursor: pointer; padding: 14px 16px; transition: 0.3s; } .tab button:hover { background-color: #ddd; } .tab button.active { background-color: #ccc; } .tabcontent { display: none; padding: 6px 12px; border: 1px solid #ccc; border-top: none; } ``` JavaScript部分: ```javascript function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablinks"); for (i = 0; i < tablinks.length; i++) { tablinks[i].className = tablinks[i].className.replace(" active", ""); } document.getElementById(tabName).style.display = "block"; evt.currentTarget.className += " active"; } ``` 解释: 通过 HTML 中的按钮元素和对应的内容区域,通过 JavaScript 的事件监听实现点击按钮显示对应的内容区域的功能。其中,openTab 函数接收两个参数,分别是事件对象 evt 和对应的选项卡名称 tabName。在函数内部,通过循环遍历所有内容区域和按钮元素,将它们的样式 display 设为 none,将所有按钮元素的类名中的 active 替换为空格。然后再将对应的内容区域样式 display 设为 block,将对应的按钮元素的类名中加上 active 样式,以示当前选中状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值