Prechádzať zdrojové kódy

商品图片放大镜预览效果

Cathy 4 rokov pred
rodič
commit
fdbc50d150
3 zmenil súbory, kde vykonal 72 pridanie a 0 odobranie
  1. 29 0
      pinyougou/css/detail.css
  2. 5 0
      pinyougou/detail.html
  3. 38 0
      pinyougou/js/detail.js

+ 29 - 0
pinyougou/css/detail.css

@@ -13,8 +13,37 @@
 	height: 590px;
 }
 .preview_img {
+	position: relative;
 	height: 398px;
 	border: 1px solid #ccc;
+	cursor: move;
+}
+.mask {
+	display: none;
+	position: absolute;
+	top: 0;
+	left: 0;
+	width: 300px;
+	height: 300px;
+	background-color: orange;
+	opacity: .5;
+}
+.big {
+	display: none;
+	position: absolute;
+	top: 0;
+	left: 400px;
+	width: 500px;
+	height: 500px;
+	overflow: hidden;
+	z-index: 999;
+}
+.big_img {
+	position: absolute;
+	top: 0;
+	left: 0;
+	width: 800px;
+	height: 800px;
 }
 .preview_list {
 	position: relative;

+ 5 - 0
pinyougou/detail.html

@@ -12,6 +12,7 @@
 	<link rel="stylesheet" href="css/detail.css">
 	<link rel="shortcut icon" href="favicon.ico">
 	<link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
+	<script src="js/detail.js"></script>
 </head>
 <body>
 	<!-- 顶部导航start  -->
@@ -212,6 +213,10 @@
 			<div class="preview_wrap fl">
 				<div class="preview_img">
 					<img src="upload/s3.png" alt="">
+					<div id="mask" class="mask"></div>
+					<div id="big" class="big">
+						<img src="upload/s3.png" alt="" class="big_img" id="big_img">
+					</div>
 				</div>
 				<div class="preview_list">
 					<a href="#" class="arrow_prev">

+ 38 - 0
pinyougou/js/detail.js

@@ -0,0 +1,38 @@
+window.onload = function () {
+  var previewImg = this.document.querySelector('.preview_img');
+  var mask = this.document.querySelector('#mask');
+  var big = this.document.querySelector('#big');
+  var bigImg = this.document.querySelector('#big_img');
+  previewImg.addEventListener('mouseover', function (e) {
+    mask.style.display = 'block';
+    big.style.display = 'block';
+  });
+  previewImg.addEventListener('mouseout', function () {
+    mask.style.display = 'none';
+    big.style.display = 'none';
+  });
+  previewImg.addEventListener('mousemove', function (e) {
+    var x = e.pageX - this.offsetLeft;
+    var y = e.pageY - this.offsetTop;
+    var mx = x - mask.offsetWidth / 2;
+    var my = y - mask.offsetHeight / 2;
+    var MaxWidth = this.offsetWidth - mask.offsetWidth;
+    var MaxHeight = this.offsetHeight - mask.offsetHeight;
+    if(mx < 0)mx = 0;
+    else if(mx > MaxWidth) {
+      mx = MaxWidth
+    }
+    if(my < 0)my = 0;
+    else if(my > MaxHeight) {
+      my = MaxHeight;
+    }
+    mask.style.left = mx + 'px';
+    mask.style.top = my + 'px';
+    var MaxWidth2 = big.offsetWidth - bigImg.offsetWidth;
+    var MaxHeight2 = big.offsetHeight - bigImg.offsetHeight;
+    var bx = mx * MaxWidth2 / MaxWidth;
+    var by = my * MaxHeight2 / MaxHeight;
+    bigImg.style.left = bx + 'px';
+    bigImg.style.top = by + 'px';
+  })
+}