<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.noselect {
-moz-user-select: none;
/*火狐*/
-webkit-user-select: none;
/*webkit浏览器*/
-ms-user-select: none;
/*IE10*/
/*早期浏览器*/
user-select: none;
}
#box {
position: relative;
}
.img-box {
width: 100%;
position: absolute;
top: 0px;
left: 0px;
}
#white {
z-index: 1;
}
#black {
z-index: 2;
overflow: hidden;
}
#move {
position: absolute;
z-index: 3;
height: 563px;
width: 5px;
background: skyblue;
transform: translate(-50%, 0);
box-sizing: border-box;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
}
#move-box {
position: absolute;
width: 60px;
height: 60px;
background: skyblue;
border-radius: 50%;
border: 2px solid #fff;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
cursor: move;
box-sizing: border-box;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<div id="box" class="noselect">
<div id="white" class="img-box white">
<img draggable="false" class="img" src="./white.jpg" alt="" />
</div>
<div id="black" class="img-box" style="width: 500px">
<img draggable="false" class="img" src="./black.jpg" alt="" />
</div>
<div id="move" style="left: 500px">
<div id="move-box"></div>
</div>
</div>
<script>
window.onload = () => {
var startX, startY;
var width = 1000;
var moveX = width / 2;
var move_box = document.getElementById("move-box");
var move = document.getElementById("move");
var black = document.getElementById("black");
var box = document.getElementById("box");
box.style.width = width + "px";
var imgs = document.querySelectorAll(".img");
for (let i = 0; i < imgs.length; i++) {
const element = imgs[i];
element.style.width = width + "px";
}
move_box.onmousedown = (me) => {
startX = me.clientX;
startY = me.clientY;
document.onmousemove = (de) => {
var endX = de.clientX;
var endY = de.clientY;
var offsetX = endX - startX;
var offsetY = endY - startY;
var newLeft = moveX + offsetX;
if (newLeft <= 0) {
move.style.left = "0px";
} else if (newLeft >= width) {
move.style.left = width + "px";
} else {
move.style.left = newLeft + "px";
}
black.style.width = move.style.left;
};
document.onmouseup = () => {
moveX = parseFloat(move.style.left.replace("px", ""));
document.onmousemove = () => {};
};
};
};
</script>
</body>
</html>
实现拖拽预览图片切换效果
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Android 1. 获取状态栏和标题栏高度 1. 实现图片的跟随手指的拖拽效果 1. 实现图片的缩放效果
- ✍️作者:极客小俊😈公众号同名: 一个把逻辑思维转变为代码的技术博主 前言 😲 刚刚学了javascript有了...
- 写在开头: 为什么要实现这么个功能,当然不是我闲得慌,当然是产品的需求。身为码农你只能想方设法去实现,即使留给你的...
- 1. 绘制多边形 在画布中以鼠标按下的点为圆心,以鼠标划过的距离为半径绘制多边形: 效果如下: 2. 实现图片反转...