怎样用jquery实现图片对比的效果?
Admin 2021-08-20 群英技术资讯 1383 次浏览
这篇文章给大家分享的是有关用jquery实现图片对比的效果的内容,这也是比较常见的效果,小编觉得挺实用的,因此分享给大家做个参考,感兴趣的朋友就跟随小编看看吧。

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>做图片对比</title>
<script src="js/jquery-3.4.1.min.js"></script>
<style>
*{
margin: 0px;
padding: 0px;
user-select: none;
}
.div{
border: 1px solid lightgray;
width: 400px;
height: 200px;
margin: 10px;
float: left;
position: relative;
}
.img1{
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 50%;
}
.img2{
position: absolute;
top: 0;
bottom: 0;
left: 50%;
right: 0;
}
.img1,.img2{
background-position: center center;
background-size: 400px 200px;
background-repeat: no-repeat;
}
.img1{
background-position-x: 0;
}
.img2{
background-position-x: 100%;
filter: invert(100%);
}
.bar{
position: absolute;
top: 0;
bottom: 0;
right:-4px;
width: 8px;
background-color: gray;
cursor: ew-resize;
opacity: 0.2;
}
.stop{
pointer-events: none;
}
</style>
</head>
<body>
<div class="div">
<div class="img1" style="background-image: url(img/1.jpg);">
<div class="bar" data-flag="0"></div>
</div>
<div class="img2" style="background-image: url(img/1.jpg);"></div>
</div>
<div class="div">
<div class="img1" style="background-image: url(img/2.jpg);">
<div class="bar" data-flag="0"></div>
</div>
<div class="img2" style="background-image: url(img/2.jpg);"></div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$(".bar").mousedown(function(){
$(this).parent().addClass("stop");
$(this).parent().next().addClass("stop");
$(this).attr("data-flag","1")
})
$(".div").mousemove(function(e){
var temp = $(this).find('.bar').attr("data-flag");
if(temp=="1"){
var w = $(this).width();
var x = e.offsetX;
var p = parseFloat((x/w).toFixed(2))*100;
$(this).children(".img1").css('width',p+'%');
$(this).children(".img2").css('left',p+'%');
}
})
$(document).mouseup(function(){
$(".img1,.img2").removeClass("stop");
$(".bar").attr("data-flag","0")
})
})
</script>
感觉很简单,就是俩图作为背景图片然后控制他的布局位置,控制容器的宽高就行了,需要做适应性优化的还有背景图大小的控制,当然父容器不会变化就不会出问题
以上就是关于jquery实现图片对比效果的介绍,希望对大家学习jquery有帮助,想要了解更多实现图片对比效果的方法,请搜索群英网络以前的文章或继续浏览下面的相关文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了JS实现圆形进度条拖拽滑动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章给大家分享的是用jQuery写一个日历插件的内容,但是本文实现的日历具有可扩展的功能,实现效果及要求如下,小编觉得挺实用的,因此分享给大家做个参考。本文有实现示例供大家参考,接下来一起跟随小编看看吧。
javascript中怎么将一维数组转为三维数组?本篇文章就来给大家分享一种转换方法,JS一维数组转化为三维数组有这个方法就够了!
&&和||总是傻傻分不清,在这里详细记录一下吧,也给你们分享一下,所以这篇文章主要给大家介绍了关于javascript中&&运算符与||运算符的使用方法,需要的朋友可以参考下
目录前提先附上成功的配置1.build/utils.js2.build/webpack.base.conf.js遇到的坑1.错误写法2.正确的写法3.如果想在“style”里面用上~@表示的相对路径前提npm install style-loader --save-devnpm install css-loader -
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008