您的位置:首页 > Web前端 > JavaScript

js实现将图片裁切成方形显示,鼠标移入放大效果

2014-12-03 13:42 1006 查看
Dome下载 效果如图:



css:

#clip { width: 306px; zoom: 1; }

#clip:after {
content: "";
display: block;
height: 0;
clear: both;
}

#clip li {
float: left;
width: 100px;
height: 100px;
margin: 1px;
position: relative;
_display: inline;
}

#clip li .small_img {
width: 100px;
height: 100px;
position: relative;
overflow: hidden;
}

html:

<ul id="clip">

<li><div class="small_img"><a href="#"><img
src="images/1.jpg" alt=""></a></div></li>

<li><div class="small_img"><a href="#"><img
src="images/2.jpg" alt=""></a></div></li>

<li><div class="small_img"><a href="#"><img
src="images/3.jpg" alt=""></a></div></li>

<li><div class="small_img"><a href="#"><img
src="images/4.jpg" alt=""></a></div></li>

<li><div class="small_img"><a href="#"><img
src="images/5.jpg" alt=""></a></div></li>

<li><div class="small_img"><a href="#"><img
src="images/6.jpg" alt=""></a></div></li>
</ul>

javascript:

<script type="text/javascript" src="script/jquery-1.8.3.min.js"></script>
<script>
$(function () {
$("#clip>li").each(function () {
var $this = $(this).find("img");
var width = $this.width();
var height = $this.height();
var newWidth,newHeight;
if (width > height) {
newWidth = width / (height / 100);
$this.css({
"position": "absolute",
"top": "0",
"left": "50%",
"margin-left": parseInt(-newWidth/2) + "px",
"height": "100px"
});
}
else if (width == height) {
$this.css({
"width": "100px"
});
}
else {
newHeight = height / (width / 100);
$this.css({
"position": "absolute",
"top": "50%",
"left": "0",
"margin-top": parseInt(-newHeight/2) + "px",
"width": "100px"
});
}

var newImg = $this.clone();

$(this).hover(
function () {
$(this).append(newImg.removeAttr("style"));
newImg.css({
"width": "202px",
"position": "absolute",
"top": "0",
"left": "0",
"z-index": "10",
"display": "none"
}).show("fast");
},
function () {
newImg.hide("fast",function () {$(this).remove()});
}
)

});
})
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: