原理:這種放大說白了就是一張大圖和一張小圖,滑塊在小圖上滑動,經過計算獲取滑塊相對於整張圖的位置,而滑塊與大圖的顯示區域是成比例的,根據比例關係將大圖的位置也調整到響應的位置,這裏對大圖位置的處理能夠絕對定位,也可用背景圖片定位,總之是要在一個較小的區域裏顯示一張大圖的一部分。css
如下使用jquery的實現,我採用的是背景圖片定位。html
HTMLjquery
<div id="glass">
<div class="left">
<div class="move-box"></div>
</div>
<div class="right"></div>
</div>
複製代碼
cssbash
body, div {
margin: 0;
padding: 0;
}
#glass {
height: 1000px;
}
.left {
margin-left: 50px;
margin-top: 50px;
width: 400px;
height: 225px;
position: relative;
background-color: #8b8b8b;
background: url(../assets/images/sky_small.png) no-repeat;
float: left;
}
.move-box {
width: 100px;
height: 56px;
position: absolute;
background-color: rgb(0, 0, 0, .5);
}
.right {
width: 600px;
height: 338px;
background-color: #8b8b8b;
background-image: url(../assets/images/sky-big.jpg);
background-repeat: no-repeat;
display: none;
float: left;
margin-left: 2px;
}
複製代碼
jqueryui
var $glass = $('#glass')
var $left = $glass.find('.left'), $right = $glass.find('.right')
var $moveBox = $glass.find('.move-box')
var moveBoxW = $moveBox.width(), moveBoxH = $moveBox.height()
var leftBoxW = $left.width(), leftBoxH = $left.height()
var rightBoxW = $right.width(), rightBoxH = $right.height()
var leftBoxX = $left.offset().left, leftBoxY = $left.offset().top
var scaleW = leftBoxW / moveBoxW, scaleH = leftBoxH / moveBoxH
var imgWidth = rightBoxW * scaleW, imgHeight = rightBoxH * scaleH
var imgSize = $right.css('background-size', imgWidth + 'px ' + imgHeight + 'px')
// 小圖的mousemove事件
$left.mousemove(function (e) {
var x = e.offsetX, y = e.offsetY
var left = x - moveBoxW / 2, top = y - moveBoxH / 2
if (left < 0) {
left = 0
}
if (left > leftBoxW - moveBoxW) {
left = leftBoxW - moveBoxW
}
if (top < 0) {
top = 0
}
if (top > leftBoxH - moveBoxH) {
top = leftBoxH - moveBoxH
}
$moveBox.css({left: left + 'px', top: top + 'px'})
imgMove($right, left, top)
}).hover(function () {
$right.css('display', 'block')
}, function () {
$right.css('display', 'none')
})
// 滑塊的mousemove事件
$moveBox.mousemove(function (e) {
e.stopPropagation()
var x = e.pageX, y = e.pageY
var left = x - leftBoxX - moveBoxW / 2, top = y - leftBoxY - moveBoxH / 2
if (left < 0) {
left = 0
}
if (left > leftBoxW - moveBoxW) {
left = leftBoxW - moveBoxW
}
if (top < 0) {
top = 0
}
if (top > leftBoxH - moveBoxH) {
top = leftBoxH - moveBoxH
}
$moveBox.css({left: left + 'px', top: top + 'px'})
imgMove($right, left, top)
})
// 大圖位置計算
var imgMove = function (ele, left, top) {
var x = -rightBoxW / moveBoxW * left + 'px', y = -rightBoxH / moveBoxH * top + 'px'
ele.css('background-position', x + ' ' + y)
}
複製代碼
這種放大方式看似在原圖上放大,其實原理也是同樣,只是如今大圖跟着滑塊在移動,一樣經過滑塊相對於小圖的位置移動大圖。url
這裏大圖移動的位置,不能單純的靠滑塊拖動的位置計算,由於滑塊的覆蓋區域沒法徹底顯示大圖的該區域,所以這裏須要以鼠標位置爲基準調整大圖位置,而後爲了將圖片顯示在滑塊中間位置,還須要根據滑塊的寬高調整大圖的位置。spa
如下是使用query實現,我採用的是背景圖片定位法。code
HTMLcdn
<div id="glass-two">
<div class="lens"></div>
</div>
複製代碼
csshtm
#glass-two {
width: 600px;
height: 338px;
position: relative;
background: url(../assets/images/sky-middle.png) no-repeat;
}
.lens {
width: 100px;
height: 100px;
position: absolute;
border-radius: 50%;
background-image: url(../assets/images/sky-big.jpg);
background-repeat: no-repeat;
border: 1px solid #000;
box-sizing: border-box;
}
複製代碼
jquery
var $glassTwo = $('#glass-two')
var $lens = $('.lens')
var glassTwoW = $glassTwo.width(), glassTowH = $glassTwo.height()
var lensW = $lens.width(), lensH = $lens.height()
var glassTowX = $glassTwo.offset().left, glassTowY = $glassTwo.offset().top
var imgWidth = 2000, imgHeight = 1225
$lens.css('background-size', `${imgWidth}px ${imgHeight}px`)
var scaleW = imgWidth / glassTwoW, scaleH = imgHeight / glassTowH
$glassTwo.mousemove(function (e) {
var x = e.offsetX, y = e.offsetY
var left = x - lensW / 2, top = y - lensH / 2
if (left < 0) {
left = 0
}
if (top < 0) {
top = 0
}
if (left > glassTwoW - lensW) {
left = glassTwoW - lensW
}
if (top > glassTowH - lensH) {
top = glassTowH - lensH
}
$lens.css({left: left + 'px', top: top + 'px'})
imgMove($lens, {W: imgWidth, H: imgHeight}, x, y)
})
$lens.mousemove(function (e) {
e.stopPropagation()
var x = e.pageX, y = e.pageY
var pointX = x - glassTowX, pointY = y - glassTowY
var left = pointX - lensW / 2, top = pointY - lensH / 2
if (left < 0) {
left = 0
}
if (top < 0) {
top = 0
}
if (left > glassTwoW - lensW) {
left = glassTwoW - lensW
}
if (top > glassTowH - lensH) {
top = glassTowH - lensH
}
$lens.css({left: left + 'px', top: top + 'px'})
imgMove($lens, {W: imgWidth, H: imgHeight}, pointX, pointY)
})
var imgMove = function ($moveBox, imgSize, left, top) {
var moveBoxW = $moveBox.width(), moveBoxH = $moveBox.height()
var x = -(scaleW * left - moveBoxW / 2), y = -(scaleH * top - moveBoxH / 2)
if (x > 0) {
x = 0
}
if (y > 0) {
y = 0
}
if (x < -(imgSize.W - moveBoxW)) {
x = -(imgSize.W - moveBoxW)
}
if (y < -(imgSize.H - moveBoxH)) {
y = -(imgSize.H - moveBoxH)
}
$moveBox.css('background-position', `${x}px ${y}px`)
}
複製代碼