重点在于设置
如何更新left top值 根据鼠标的移动
function bigImg() {
var $mask = $('#mask'); //遮挡需要加显示
var $mediuImg = $('#mediuImg'); //鼠标移入图片需要控制效黄块的显示
var $loading = $('#loading');
var $largeImg = $('#largeImg');
var $largeImgContainer = $('#largeImgContainer');
var $mediuImgContainer = $('#mediuImgContainer');
var $mediuImgContainerHeight = $mediuImgContainer.height();
var $mediuImgContainerWidth = $mediuImgContainer.width();
$mediuImgContainer.hover(function() {
//显示 并得出left top值
$mask.show();
//动态加载大图 加载对应大图的src
var src = $mediuImg.attr('src');
$largeImgContainer.show();
//图片的加载完成需要针对图片是否加载成功
//on机制--加载单一图片
$largeImg.attr('src', src);
//只有在图片加载完成后才有意义
$largeImg.on('load', function() {
//监听鼠标移动事件
$mediuImgContainer.mousemove(function(event) {
//在移动过程中不断调用
var left = 0;
var top = 0;
//给小方块定位
//鼠标的位置
left = event.clientX - $mediuImgContainer.offset().left - $mask.width() / 2;
top = event.clientY - $mediuImgContainer.offset().top - $mask.height() / 2;
//区间需要有限制 0-$mediuImgContainerWidth-$mask.width()
//0-0-$mediuImgContainerHeight-$mask.height()
if (left < 0) {
left = 0
} else if (left > $mediuImgContainerWidth - $mask.width()) {
left = $mediuImgContainerWidth - $mask.width();
}
if (top < 0) {
top = 0
} else if (top > $mediuImgContainerHeight - $mask.height()) {
top = $mediuImgContainerHeight - $mask.height();
}
$mask.css({
left: left,
top: top
});
//移动大图片
//区间需要有限制 0-$mediuImgContainerWidth-$mask.width() 2倍关系
//0-0-$mediuImgContainerHeight-$mask.height()
left = left * 2;
top = top * 2;
//设置大图的坐标
if (left < 0) {
left = 0
} else if (left > ($mediuImgContainerWidth - $mask.width()) * 2) {
left = (left > ($mediuImgContainerWidth - $mask.width()) * 2) * 2
}
if (top < 0) {
top = 0
} else if (top > ($mediuImgContainerHeight - $mask.height()) * 2) {
top = (top > $mediuImgContainerHeight - $mask.height()) * 2
}
$largeImg.css({
left: -left,
top: -top
});
//显示大图
$largeImg.show();
//隐藏加载记录条
$loading.hide();
});
});
},
function() {
$mask.hide();
$largeImgContainer.hide();
});
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="../js/jquery-3.6.1.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
#product_intro {
position: relative;
height: 600px;
width: 520px;
left: 100px;
top: 100px;
}
#mediuImg {
width: 520px;
height: 450px;
}
.backward,
.forword {
display: inline-block;
font-size: 60px;
width: 60px;
height: 150px;
line-height: 150px;
background: yellow;
float: left;
text-decoration: none;
overflow: hidden;
}
.forword {
position: absolute;
left: 0px;
top: 450px;
}
.backward {
position: absolute;
left: 460px;
top: 450px;
}
.color {
width: 60px;
height: 150px;
background-color: black;
}
#tmp {
position: absolute;
float: left;
width: 450px;
height: 150px;
top: 450px;
overflow: hidden;
left: 60px;
background: #000;
}
#icon_list {
position: absolute;
height: 150px;
width: 800px;
float: left;
font-size: 0px;
}
#icon_list li {
width: 100px;
height: 150px;
font-size: 30px;
font-weight: bold;
text-align: center;
line-height: 150px;
display: inline-block;
}
#mediuImgContainer {
position: absolute;
}
#mask {
width: 200px;
height: 200px;
background-color: coral;
position: absolute;
left: 0px;
opacity: 0.5;
display: none;
}
#largeImgContainer {
position: absolute;
width: 400px;
height: 400px;
left: 530px;
top: 200px;
overflow: hidden;
display: none;
}
#largeImgContainer img {
/* 两倍关系 */
width: 1040px;
/* 520 */
height: 900px;
/* 450 */
display: none;
}
#largeImg {
position: absolute;
top: 800px;
}
</style>
</head>
<body>
<div id="product_intro">
<span id="mediuImgContainer">
<img id="mediuImg" src="../../img/a0.png" alt="图片">
<span id="mask"></span><!-- 遮挡 -->
</span>
<span id="largeImgContainer">
<img id="loading" src="../../img/load.png" alt="未加载成功时候的显示"> <!-- 显示一个预加载的状态 网不好的状态显示 -->
<img id="largeImg" alt="显示其中的1/4">
</span>
<div id="tmp">
<ul id="icon_list">
<li style="background: #2ab;">1</li>
<li style="background: #bfa;">2</li>
<li style="background: #acb;">3</li>
<li style="background: #897;">4</li>
<li style="background: #345;">5</li>
<li style="background: #a56;">6</li>
<li style="background: #189;">7</li>
<li style="background: #c78;">8</li>
</ul>
</div>
<!-- 点击监听 -->
<a class="backward" href="#">→</a>
<a class="forword" href="#">←</a>
</div>
<script>
moveMiniImg();
hoverMiniImg();
bigImg()
function bigImg() {
var $mask = $('#mask'); //遮挡需要加显示
var $mediuImg = $('#mediuImg'); //鼠标移入图片需要控制效黄块的显示
var $loading = $('#loading');
var $largeImg = $('#largeImg');
var $largeImgContainer = $('#largeImgContainer');
var $mediuImgContainer = $('#mediuImgContainer');
var $mediuImgContainerHeight = $mediuImgContainer.height();
var $mediuImgContainerWidth = $mediuImgContainer.width();
$mediuImgContainer.hover(function() {
//显示 并得出left top值
$mask.show();
//动态加载大图 加载对应大图的src
var src = $mediuImg.attr('src');
$largeImgContainer.show();
//图片的加载完成需要针对图片是否加载成功
//on机制--加载单一图片
$largeImg.attr('src', src);
//只有在图片加载完成后才有意义
$largeImg.on('load', function() {
//监听鼠标移动事件
$mediuImgContainer.mousemove(function(event) {
//在移动过程中不断调用
var left = 0;
var top = 0;
//给小方块定位
//鼠标的位置
left = event.clientX - $mediuImgContainer.offset().left - $mask.width() / 2;
top = event.clientY - $mediuImgContainer.offset().top - $mask.height() / 2;
//区间需要有限制 0-$mediuImgContainerWidth-$mask.width()
//0-0-$mediuImgContainerHeight-$mask.height()
if (left < 0) {
left = 0
} else if (left > $mediuImgContainerWidth - $mask.width()) {
left = $mediuImgContainerWidth - $mask.width();
}
if (top < 0) {
top = 0
} else if (top > $mediuImgContainerHeight - $mask.height()) {
top = $mediuImgContainerHeight - $mask.height();
}
$mask.css({
left: left,
top: top
});
//移动大图片
//区间需要有限制 0-$mediuImgContainerWidth-$mask.width() 2倍关系
//0-0-$mediuImgContainerHeight-$mask.height()
left = left * 2;
top = top * 2;
//设置大图的坐标
if (left < 0) {
left = 0
} else if (left > ($mediuImgContainerWidth - $mask.width()) * 2) {
left = (left > ($mediuImgContainerWidth - $mask.width()) * 2) * 2
}
if (top < 0) {
top = 0
} else if (top > ($mediuImgContainerHeight - $mask.height()) * 2) {
top = (top > $mediuImgContainerHeight - $mask.height()) * 2
}
$largeImg.css({
left: -left,
top: -top
});
//显示大图
$largeImg.show();
//隐藏加载记录条
$loading.hide();
});
});
},
function() {
$mask.hide();
$largeImgContainer.hide();
});
}
function hoverMiniImg() {
$('#icon_list>li').hover(function() {
//鼠标移入
$(this).css('color', 'red'); //添加样式
//显示对应的中图
var src = "../../img/a" + $(this).index() + ".png";
$('#mediuImg').attr('src', src)
}, function() {
//鼠标移出
$(this).css('color', 'black'); //添加样式
});
}
function moveMiniImg() {
var $aa = $('#product_intro>a');
var $backward = $('.backward');
var $forword = $('.forword');
var $ul = $('#icon_list');
var showPic = 4; //一次性显示4个
var imgCount = $ul.children().length; //所有图片数量
var distance = 100;
//初始化更新
if (imgCount > showPic) {
$forword.addClass('color')
}
//向右的绑定监听
var moveCount = 0; //点击向右+1 点击向左 -1
$backward.click(function() {
//判断是否需要移动 不需要直接不行
if (moveCount === imgCount - showPic) { //4个
$backward.addClass('color');
return
}
moveCount++;
//更新右边移动的按钮
$forword.removeClass('color');
//更新右边的按钮
if (moveCount === imgCount - showPic) {
$backward.addClass('color');
}
//移动图片的距离
$ul.css('left', -distance * moveCount);
})
//更新左边移动的按钮
$forword.click(function() {
//判断是否需要移动 不需要直接不行
if (moveCount === 0) { //一上来不能移动 是0
$forword.addClass('color');
return
}
moveCount--;
//更新右边移动的按钮
$backward.removeClass('color');
//更新左边的按钮
if (moveCount === 0) {
$forword.addClass('color');
}
//移动图片的距离
$ul.css('left', -(moveCount) * distance);
})
}
</script>
</body>
</html>