实现图片分割---产生多个div分割图片 使用for和二维数组来设置背景定位
2014-10-25 16:46
891 查看
图片展示
1.二维函数写法【效果展示】
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
body{background:#939393;}
#box{width:800px;height:800px;margin:0 auto;background:url(11.jpg);overflow: hidden;}
.col{width:50px;height:50px;outline:1px solid #fff;float:left;background:url(00.jpg) no-repeat;opacity:0;filter:alpha(opacity:0);}
</style>
</head>
<body>
<div id="box"></div>
</body>
</html>
<script type="text/javascript">
var box=document.getElementById("box");
var rowDiv=[];
var tArray=new Array();
for(var i=0;i<16;i++) {
tArray[i] = new Array();
for (var j = 0; j < 16; j++) {
rowDiv[j] = document.createElement("div");
box.appendChild(rowDiv[j]);
rowDiv[j].className = "col";
tArray[i][j] = rowDiv[j];
var l = -(j * 50) + "px";
var T = -(i * 50) + "px";
tArray[i][j].style.backgroundPosition =l+" "+T;
tArray[i][j].onmouseover = function ()
{
this.style.opacity = "1";
this.style.filter="alpha(opacity:100)";
}
}
}
</script>
2.字符串写法【效果展示】
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
body{background:#939393;}
#box{width:800px;height:800px;margin:0 auto;background:url(11.jpg);overflow: hidden;}
.col{width:50px;height:50px;outline:1px solid #fff;float:left;background:url(00.jpg) no-repeat;opacity:0;filter:alpha(opacity:0);}
</style>
</head>
<script type="text/javascript" src="jquery-1.10.1.min.js"></script>
<body>
<div id="box"></div>
</body>
</html>
<script type="text/javascript">
var box=document.getElementById("box");
var col=box.getElementsByTagName("div");
var rowDiv=[];
var bg_P=[];
var str="";
for(var i=0;i<16;i++)
{
for(var j=0;j<16;j++)
{
str+='<div class="col" style="background-position:'+-(j*50)+'px '+-(i*50)+'px"></div>';
}
}
box.innerHTML=str;
for(var i=0;i<col.length;i++)
{
col[i].onmouseover=function(){
this.style.opacity="1";
this.style.filter="alpha(opacity:100)";
}
}
</script>
1.二维函数写法【效果展示】
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
body{background:#939393;}
#box{width:800px;height:800px;margin:0 auto;background:url(11.jpg);overflow: hidden;}
.col{width:50px;height:50px;outline:1px solid #fff;float:left;background:url(00.jpg) no-repeat;opacity:0;filter:alpha(opacity:0);}
</style>
</head>
<body>
<div id="box"></div>
</body>
</html>
<script type="text/javascript">
var box=document.getElementById("box");
var rowDiv=[];
var tArray=new Array();
for(var i=0;i<16;i++) {
tArray[i] = new Array();
for (var j = 0; j < 16; j++) {
rowDiv[j] = document.createElement("div");
box.appendChild(rowDiv[j]);
rowDiv[j].className = "col";
tArray[i][j] = rowDiv[j];
var l = -(j * 50) + "px";
var T = -(i * 50) + "px";
tArray[i][j].style.backgroundPosition =l+" "+T;
tArray[i][j].onmouseover = function ()
{
this.style.opacity = "1";
this.style.filter="alpha(opacity:100)";
}
}
}
</script>
2.字符串写法【效果展示】
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{margin:0;padding:0;}
body{background:#939393;}
#box{width:800px;height:800px;margin:0 auto;background:url(11.jpg);overflow: hidden;}
.col{width:50px;height:50px;outline:1px solid #fff;float:left;background:url(00.jpg) no-repeat;opacity:0;filter:alpha(opacity:0);}
</style>
</head>
<script type="text/javascript" src="jquery-1.10.1.min.js"></script>
<body>
<div id="box"></div>
</body>
</html>
<script type="text/javascript">
var box=document.getElementById("box");
var col=box.getElementsByTagName("div");
var rowDiv=[];
var bg_P=[];
var str="";
for(var i=0;i<16;i++)
{
for(var j=0;j<16;j++)
{
str+='<div class="col" style="background-position:'+-(j*50)+'px '+-(i*50)+'px"></div>';
}
}
box.innerHTML=str;
for(var i=0;i<col.length;i++)
{
col[i].onmouseover=function(){
this.style.opacity="1";
this.style.filter="alpha(opacity:100)";
}
}
</script>
相关文章推荐
- 图片切割工具---产生多个div切割图片 采用for和一的二维阵列设置背景位置
- 设置div的背景为半透明,下面有个不错示例,使用div+css来实现下,不会的朋友可以参考下
- css3实现一个div设置多张背景图片及background-image属性
- css3实现一个div设置多张背景图片及background-image属性
- css3实现一个div设置多张背景图片及background-image属性
- div+css使用backgroundRepeat样式设置背景图片的显示方式
- 图片(图片路径需分割)使用Universal-Image-Loader加载并设置内存和sd卡缓存,导入XListView库,并实现下拉刷新、上拉加载更多。
- css3实现一个div设置多张背景图片及background-image属性
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- ASP.NET控件使用——Gridview设置表头的背景图片
- Delphi中实现设置TEdit透明背景图片效果 本文来自Delphi之窗
- div+css 背景图片的定位取图问题详解
- symbian for Qt 设置背景图片
- 一列固定宽度布局和背景图片绝对定位的实现代码
- Qt中设置widget背景颜色/图片的注意事项(使用样式表 setStyleSheet())
- div+css 背景图片的定位取图方法
- 使用BorderContainer设置背景图片
- div+css 背景图片的定位取图问题详解
- 使用 IActiveDesktop 接口获取设置桌面背景图片
- 一个图片轮换显示的实现,使用DIV+JS,支持Firefox,IE,Safiri