<!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;
}
/* 边框 */
#main {
width: 635px;
height: 310px;
/* border:1px solid blue; */
}
#main #content img {
max-height: 310px;
max-width: 310px;
vertical-align: middle;
}
#show{
display: inline-block;
height: 310px;
width: 310px;
border: 1px solid silver;
/* text-align: center; */
}
a{
display: inline-block;
border: 1px yellow solid;
}
#content {
float: left;
position: relative;
/* border: 1px solid red; */
width: 310px;
height: 310px;
}
#square {
cursor:pointer;
background:url('./img/mask2.png');
/* opacity: 0.5; */
left: 0px;
top: 0px;
position: absolute;
display: none;
}
#enlarge {
float: right;
width: 310px;
height: 310px;
/* background-color: red; */
background: url('./img/05.jpg') no-repeat;
display: none;
border: 1px silver solid;
}
</style>
</head>
<body>
<div id="main">
<!-- 正方块310px -->
<div id="content">
<!-- 要展示的图片 -->
<a id='show'>
<img id="midImg" src="./img/05_mid.jpg" alt="">
</a>
<!-- 小正方块 -->
<div id="square">
</div>
</div>
<!-- 放大的区域 正方块310px -->
<div id="enlarge">
</div>
</div>
<div>
<img style="display: none;" id="bigImg" src="./img/05.jpg" />
</div>
<script>
var content = document.getElementById('content');
var square = document.getElementById('square');
var enlarge = document.getElementById('enlarge');
var midImg = document.getElementById('midImg');
var bigImg = document.getElementById('bigImg');
var show = document.getElementById('show');
content.onmouseover = function () {
// alert(square.offsetHeight)
// square.style.width = a + 'px';
// square.style.height = b + 'px';
enlarge.style.display = 'block';
square.style.display = 'block';
// midImg/bigImg=square/enlarge;
}
content.onmouseout = function () {
enlarge.style.display = 'none';
square.style.display = 'none';
}
content.onmousemove = function (e) {
//clientWidth不带边框
// alert(bigImg.width)
//设置蓝色小方块的大小
var n=enlarge.offsetWidth/(bigImg.width/midImg.width);
square.style.width=n+'px';
square.style.height=n+'px';
// alert(square.style.width)
var _event = e || window.event;
var mouseLeft = _event.clientX;
var mouseTop = _event.clientY;
var squareLeft = mouseLeft - square.offsetWidth / 2;
var squareTop = mouseTop - square.offsetHeight / 2;
if (squareLeft >= midImg.width - square.offsetWidth) {
squareLeft = midImg.width - square.offsetWidth;
}
if (squareTop >= midImg.height - square.offsetHeight) {
squareTop = midImg.height - square.offsetHeight;
}
if (squareTop < 0) {
squareTop = 0;
}
if (squareLeft < 0) {
squareLeft = 0;
}
square.style.left = squareLeft + 'px';
square.style.top = squareTop + 'px';
var scaleX = squareLeft / midImg.width;
var scaleY = squareTop / midImg.height;
var enlargeLeft= scaleX * bigImg.width;
var enlargeTop= scaleY * bigImg.height;
enlarge.style.backgroundPosition='-'+enlargeLeft+'px -'+enlargeTop+'px';
}
</script>
</body>
</html>
JavaScript鼠标悬停放大demo1
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 了解Mockplus的用户会知道,该原型工具目前并不直接支持鼠标悬停功能。但我经过尝试,发现想用它实现一个鼠标悬停...
- AppKit和UIKit还是有很多区别的,国内的资源比较少,做Mac开发的人也很少,导致我学的过程中常常伴着各种翻...
- 最近在慕课网报名参加了一门web自动化测试课程,使用的是java语言,对于习惯了python的自己来说,学习起来有...