css ul li图片带边框文字混排CSS+Jquery (扩展版)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<link href="s_common.css" type="text/css" rel="Stylesheet" />
<script type="text/javascript" src="jquery-1.4.2.min.js"> </script>


<style type="text/css">




#k_img_ul{ float:left; width:1000px; padding:18px 0 0 0; }/*通过这个ul来控制这个部分与外部的距离*/
#k_img_ul li{ float:left; width:64px; padding:0 5px; text-align:center;}
#k_img_ul li span{ width:60px; height:120px; padding:2px; display:block; border:1px solid #ccc; }/*如果不带边框可以不要这个span*/
#k_img_ul li span a{ display:block; width:60px; height:120px; overflow:hidden; }/*重点是红色部分*/
#k_img_ul p{ height:24px; line-height:24px;}

</style>


</head>
<body>
<ul id="k_img_ul">
<li><span><a href="#"><img src="5.jpg" /></a></span><p>king</p></li>
<li><span><a href="#"><img src="new.jpg" id="another" /></a></span><p>king</p></li>
<li><span><a href="#"><img src="na.jpg" id="im" /></a></span><p>king</p></li>
<li><span><a href="#"><img src="5.jpg" /></a></span><p>king</p></li>

<br class="clear" />
</ul>

</body>

</html>
<script type="text/javascript">

$(document).ready(function(){

//长宽任意设定

function makeMid(className,width,height){//参数为要对其进行操作的图片元素,设定的高度,设定的宽度
var h=className.height();//图片原本的高度
var w=className.width();//图片原本的宽度
// alert("h:"+h+"w:"+w);


if((h<height)&&(w>width))//图片原本高度小于定值,而宽度大于定值
{
var marginT=Math.round(height/2-h/2);
marginT=marginT+"px 0 0 0";
className.css({width:width,height:h,margin:marginT });//这种情况下宽度为指定宽度
return;
}
else if((h<height)&&(w<width))//图片原本的宽高均小于定值
{
var marginTT=Math.round(height/2-h/2);
marginTT=marginTT+"px 0 0 0";
className.css({width:w ,height:h,margin:marginTT});//当宽高均小于定宽时,宽度为原图片宽度
return;
}





}



window.οnlοad=function(){

$("#k_img_ul>li>span>a>img ").each (function(){//这里要注意要把元素层级写明,一个也不能少还有就是each相当于循环的功能
makeMid($(this),Number(60),Number(120));

});

}


});
</script>

效果图如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值