gpt4 book ai didi

javascript - HTML5 之前和之后比较 slider

转载 作者:行者123 更新时间:2023-11-28 01:30:36 25 4
gpt4 key购买 nike

有很多“前后比较 slider ”。我有兴趣改进它。

是否可以在不点击的情况下滑动图像?我的意思是,divisor(dragger) 将链接到鼠标。当用户只需将鼠标悬停(而不像现在那样单击和拖动)时,幻灯片就会发生变化。

引用资料如下:

https://codepen.io/dudleystorey/pen/HkwBo

https://codyhouse.co/gem/css-jquery-image-comparison-slider (点击演示)

<div id="comparison">
<figure>
<div id="divisor"></div>
</figure>
<input type="range" min="0" max="100" value="50" id="slider" oninput="moveDivisor()">
</div>


/**CSS**/
div#comparison {
width: 60vw;
height: 60vw;
max-width: 600px;
max-height: 600px;
overflow: hidden;
}
div#comparison figure {
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-before.jpg);
background-size: cover;
position: relative;
font-size: 0;
width: 100%;
height: 100%;
margin: 0;
}
div#comparison figure > img {
position: relative;
width: 100%;
}
div#comparison figure div {
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-after.jpg);
background-size: cover;
position: absolute;
width: 50%;
box-shadow: 0 5px 10px -2px rgba(0,0,0,0.3);
overflow: hidden;
bottom: 0;
height: 100%;
}

input[type=range]{
-webkit-appearance:none;
-moz-appearance:none;
position: relative;
top: -2rem; left: -2%;
background-color: rgba(255,255,255,0.1);
width: 102%;
}
input[type=range]:focus {
outline: none;
}
input[type=range]:active {
outline: none;
}

input[type=range]::-moz-range-track {
-moz-appearance:none;
height:15px;
width: 98%;
background-color: rgba(255,255,255,0.1);
position: relative;
outline: none;
}
input[type=range]::active {
border: none;
outline: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance:none;
width: 20px; height: 15px;
background: #fff;
border-radius: 0;
}
input[type=range]::-moz-range-thumb {
-moz-appearance: none;
width: 20px;
height: 15px;
background: #fff;
border-radius: 0;
}
input[type=range]:focus::-webkit-slider-thumb {
background: rgba(255,255,255,0.5);
}
input[type=range]:focus::-moz-range-thumb {
background: rgba(255,255,255,0.5);
}


/**JS**/
var divisor = document.getElementById("divisor"),
slider = document.getElementById("slider");
function moveDivisor() {
divisor.style.width = slider.value+"%";
}

最佳答案

是的——这在图形比较网站和 Buzzfeed/ClickHole 等地方已经很普遍了。

我修改了您的 CodePen 演示以使用 mousemove事件,因此不需要点击/拖动:

var divisor = document.getElementById("divisor");
var comparisonWidth = document.getElementById("comparison").clientWidth;

function moveDivisor(e) {
divisor.style.width = (e.offsetX * 100 / comparisonWidth) + "%";
}
div#comparison {
width: 60vw;
height: 60vw;
max-width: 600px;
max-height: 600px;
overflow: hidden;
}

div#comparison figure {
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-before.jpg);
background-size: cover;
position: relative;
font-size: 0;
width: 100%;
height: 100%;
margin: 0;
}

div#comparison figure>img {
position: relative;
width: 100%;
}

div#comparison figure div {
background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-after.jpg);
background-size: cover;
position: absolute;
width: 50%;
box-shadow: 0 5px 10px -2px rgba(0, 0, 0, 0.3);
overflow: hidden;
bottom: 0;
height: 100%;
}
<div id="comparison" onmousemove="moveDivisor(event)">
<figure>
<div id="divisor"></div>
</figure>
</div>

关于javascript - HTML5 之前和之后比较 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51003696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com