如果您具有border-radius
組的元素中的圖像,你要隱藏的圖像的「角落」,您需要設置border-radius
的圖像匹配。
但在你的情況下,這將無法正常工作,因爲你的圖像比你的包含元素大得多。最好是使用<div>
作爲鏡頭,並設置background-image
以匹配您的圖像。
演示:http://jsfiddle.net/ThinkingStiff/wQyLJ/
HTML:
<div id="image-frame">
<img id="image" src="http://thinkingstiff.com/images/matt.jpg" />
<div id="lens" ></div>
<div>
CSS:
#image-frame {
position: relative;
}
#lens {
background-repeat: no-repeat;
border-radius: 150px;
height: 150px;
position: absolute;
width: 150px;
}
腳本:
document.getElementById('image-frame').addEventListener('mousemove', function (event) {
var lens = document.getElementById('lens'),
image = document.getElementById('image'),
radius = lens.clientWidth/2,
imageTop = this.documentOffsetTop,
imageLeft = this.documentOffsetLeft,
zoom = 4,
lensX = (event.pageX - radius - imageLeft) + 'px',
lensY = (event.pageY - radius - imageTop) + 'px',
zoomWidth = (image.clientWidth * zoom) + 'px',
zoomHeight = (image.clientHeight * zoom) + 'px',
zoomX = -(((event.pageX - imageLeft) * zoom) - radius) + 'px',
zoomY = -(((event.pageY - imageTop) * zoom) - radius) + 'px';
if(event.pageX > imageLeft + image.clientWidth
|| event.pageX < imageLeft
|| event.pageY > imageTop + image.clientHeight
|| event.pageY < imageTop ) {
lens.style.display = 'none';
} else {
lens.style.left = lensX;
lens.style.top = lensY;
lens.style.backgroundImage = 'url(' + image.src + ')';
lens.style.backgroundSize = zoomWidth + ' ' + zoomHeight;
lens.style.backgroundPosition = zoomX + ' ' + zoomY;
lens.style.display = 'block';
};
}, false);
window.Object.defineProperty(Element.prototype, 'documentOffsetTop', {
get: function() {
return this.offsetTop + (this.offsetParent ? this.offsetParent.documentOffsetTop : 0);
}
});
window.Object.defineProperty(Element.prototype, 'documentOffsetLeft', {
get: function() {
return this.offsetLeft + (this.offsetParent ? this.offsetParent.documentOffsetLeft : 0);
}
});
輸出:
你是否已將div的'overflow'屬性設置爲'hidden'? – 2012-01-06 19:56:39
@BrianDriscoll,'overflow:hidden'不適用於chrome(或其他webkit瀏覽器)中絕對定位元素的圓角。 – 2012-01-06 19:57:38
@ techie_28,這裏有人和你有同樣的問題。他們沒有回答,但他們解決了它:http://stackoverflow.com/questions/5736503/how-to-make-css3-round-corners-hide-overflow-in-chrome-opera – 2012-01-06 19:58:12