Nếu bạn có một hình ảnh bên trong một phần tử có border-radius
bộ, và bạn muốn ẩn "góc" của hình ảnh, bạn cần phải thiết lập border-radius
vào hình ảnh để phù hợp.
Nhưng trong trường hợp của bạn sẽ không hoạt động vì hình ảnh của bạn lớn hơn nhiều so với phần tử chứa của bạn. Tốt hơn là sử dụng <div>
làm ống kính và đặt background-image
để phù hợp với hình ảnh của bạn.
Demo: 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;
}
Script:
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);
}
});
Output:
bạn có đặt thuộc tính 'tràn 'của div thành' hidden' không? –
@BrianDriscoll, 'overflow: hidden' không hoạt động cho các góc được làm tròn trong các phần tử được định vị hoàn toàn trong chrome (hoặc các trình duyệt webkit khác). –
@ techie_28, đây là một người có cùng vấn đề với bạn. Họ không trả lời, nhưng họ đã làm việc xung quanh nó: http://stackoverflow.com/questions/5736503/how-to-make-css3-rounded-corners-hide-overflow-in-chrome-opera –