找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2665|回复: 4

[教程]  JS效果之图片切割

[复制链接]
发表于 2012-1-5 09:25:21 | 显示全部楼层 |阅读模式 来自 中国–江苏–南京

JS效果之图片切割
DEMO1(随机出现): http://cnwander.com/demo/puzzle_effect/
机器配置好的可以将图片切得更细一点 (变量xNum,yNum)
DEMO1(规则运动): http://cnwander.com/demo/puzzle_effect/index2.html
测试:IE6、IE7、FF3.014
原文:http://cnwander.com/blog/?p=13
突发奇想的效果,主要突出构思,效果还比较粗糙,好好创意一下,应该可以引申出一些比较有新意的图片切换效果。
没什么特别的东西,至于思路,有兴趣的同学自己看DEMO吧。
效果一(随机出现):
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Puzzle effect - by Wander</title>
  6. <style type="text/css">
  7. * {margin:0; padding:0}
  8. ul {list-style:none}
  9. img {border:0}
  10. body {position:relative; background:white; text-align:center; font:12px/120% Arial,simsun,sans-serif; color:white; }
  11. .imgWrap {margin:0 auto}
  12. #thumbs {position:absolute; top: 600px; left:300px; width:400px}
  13. #thumbs li {float:left; margin:0 5px; cursor:pointer}
  14. #thumbs li img {float:left}
  15. #thumbs li.current {margin:-3px 2px 0 2px; padding:1px; border:2px solid black}
  16. </style>
  17. <script type="text/javascript">
  18. //code by Wander @ cnwander.com
  19. var xNum = 2,
  20. yNum = 2,
  21. xLimit,
  22. yLimit;;
  23. var thumbs,wrap;
  24. window.onload = function() {
  25. var thumbsWrap = $("#thumbs");
  26. setStyle(thumbsWrap,"left",($("body").clientWidth - thumbsWrap.offsetWidth)/2+"px");
  27. xLimit = [0,$("body").clientWidth];
  28. yLimit = [0,screen.height - 200];
  29. thumbs = $("#thumbs li");
  30. loadImg(thumbs[0].getAttribute("url"));
  31. each(thumbs,thumbEvts);
  32. }
  33. function thumbEvts() {
  34. this.onclick = thumbDown;
  35. function thumbDown(e) {
  36.   each(thumbs,function(){
  37.    this.className = "";
  38.   });
  39.   this.className = "current";
  40.   loadImg(this.getAttribute("url"));
  41. }
  42. }
  43. function loadImg(url) {
  44. var img = new Image();
  45. var left,
  46.   top = 100,
  47.   imgW,imgH;
  48. if(wrap) removeElem(wrap);
  49. wrap = addDiv($("body"),{className:"imgWrap"});
  50. img.src = url;
  51. wrap.innerHTML = "<img src="/articleimg/2009/11/7205/loading.gif" style="margin-top:250px"/>";
  52. if(img.width > 0) initImg();
  53. else img.onload = initImg;

  54. function initImg() {
  55.   imgW = img.width;
  56.   imgH = img.height;
  57.   left = ($("body").clientWidth - imgW)/2;
  58.   wrap.innerHTML = "";
  59.   numTimer = 0;
  60.   for(var i=0; i<xNum; i++) {
  61.    for(var j=0; j<yNum; j++)
  62.     addPiece(i,j);
  63.   }
  64. }

  65. function addPiece(x,y) {
  66.   var obj = addDiv(wrap),
  67.    w = imgW/xNum,
  68.    h = imgH/yNum,
  69.    opa = 0,
  70.    xPos = x*w,
  71.    yPos = y*h,
  72.    xStart = randowNum(xLimit[0],xLimit[1]-w),
  73.    yStart = randowNum(yLimit[0],yLimit[1]-h);   
  74.   
  75.   slideTo();
  76.   
  77.   function slideTo() {
  78.    var a = 0.2,
  79.     toScale = 1,
  80.     toX = left+xPos,
  81.     toY = top+yPos,
  82.     toOpa = 1,
  83.     timer = window.setInterval(move,20);
  84.    
  85.    numTimer++;
  86.    function move() {
  87.     xStart += (toX - xStart)*a;
  88.     yStart += (toY - yStart)*a;
  89.     opa += (toOpa - opa)*a;
  90.     if(Math.abs(toX-xStart)<=1) {
  91.      xStart = toX;
  92.      yStart = toY;
  93.      opa = toOpa;
  94.      window.clearInterval(timer);
  95.      numTimer--;
  96.      if(numTimer <= 0) {
  97.       setStyle(wrap,"paddingTop",top+"px");
  98.       wrap.innerHTML = "<img src=""+url+""/>";
  99.      }
  100.     }
  101.     obj.style.cssText = "position:absolute;left:"+xStart+"px;top: "+yStart+"px;width:"+w+"px;height:"+h+"px;background:url("+url+") no-repeat -"+xPos+"px -"+yPos+"px;filter:alpha(opacity="+ opa*100 +"); -moz-opacity:"+ opa +"; opacity:"+ opa;
  102.    
  103.    }
  104.   }
  105.   
  106. }
  107. }
  108. function addDiv(where,attr) {
  109. var obj = document.createElement("div");
  110. if(attr)
  111. for(var key in attr) {
  112.   if(key == "className") obj.className = attr[key];
  113.   else obj.setAttribute(key,attr[key]);
  114. }
  115. where.appendChild(obj);
  116. return obj;
  117. }
  118. function removeElem(elem){
  119. var parent = elem.parentNode;
  120. if(parent){
  121.   parent.removeChild(elem);
  122. }
  123. }
  124. //遍历对象
  125. function each(obj,fuc) {
  126. for(var key in obj) {
  127.   obj[key].index = key;
  128.   fuc.call(obj[key]);  
  129. }  
  130. }
  131. function randowNum(start,end) {
  132. return Math.floor(Math.random()*(end - start)) + start;
  133. }
  134. //设置样式
  135. function setStyle() {
  136. if(arguments.length == 2 &&  typeof arguments[1] == "object") {
  137.   for(var key in arguments[1]) {
  138.    arguments[0].style[key] = arguments[1][key];
  139.   }
  140. } else if (arguments.length > 2) {
  141.   arguments[0].style[arguments[1]] = arguments[2];
  142. }
  143. }
  144. //elements finder
  145. function $(s,wrap) {
  146. var eleExpr =  /([a-zA-Z0-9]*)([#\.]?)(\w+)[^\s+]*/g,
  147.   result = [wrap || document];
  148. while(eleExpr.test(s)) {
  149.   var tag = RegExp.$1,
  150.    type = RegExp.$2,
  151.    content = RegExp.$3,
  152.    elems = [];
  153.   for(var i=0; i<result.length; i++) {
  154.    var temp;
  155.    switch(type) {
  156.     case "#":
  157.      temp = [result[i].getElementById(content)];
  158.      break;
  159.     case ".":
  160.      temp = getElemsByClassName(content,result[i],tag);
  161.      break;
  162.     default:
  163.      temp = result[i].getElementsByTagName(tag+content);
  164.    }
  165.    for(var key in temp) {
  166.     if(temp[key].nodeType == 1) elems.push(temp[key]);
  167.    }     
  168.   }
  169.   result = [];
  170.   for(var key in elems) {
  171.    if(elems[key].nodeType == 1) result.push(elems[key]);
  172.   }
  173. }
  174. return result.length == 1 ? result[0] : result;
  175. function getElemsByClassName(classname,elem,tag) {
  176.   var result = [],
  177.    tag = tag || "*",
  178.    allElems = elem.getElementsByTagName(tag) || elem.all;
  179.   for(var i=0; i<allElems.length; i++){
  180.    var list = allElems[i].className.split(" ");
  181.    for(var j=0; j<list.length; j++){
  182.     if(list[j] == classname) result.push(allElems[i]);
  183.    }      
  184.   }
  185.   return result;
  186. }
  187. }
  188. </script>
  189. </head>
  190. <body>
  191. <div style="position:absolute; right:0"><a href="http://cnwander.com/blog/?p=13">Wander's space</a></div>
  192. <ul id="thumbs">
  193. <li url="/articleimg/2009/11/7205/photo01.jpg" class="current"><img src="/articleimg/2009/11/7205/thumb01.jpg" alt=""/></li>
  194. <li url="/articleimg/2009/11/7205/photo02.jpg"><img src="/articleimg/2009/11/7205/thumb02.jpg" alt=""/></li>
  195.     <li url="/articleimg/2009/11/7205/photo03.jpg"><img src="/articleimg/2009/11/7205/thumb03.jpg" alt=""/></li>
  196. </ul>
  197. </body>
  198. </html>
复制代码

评分

参与人数 1金币 +1 收起 理由
光明使者 + 1 很给力!

查看全部评分

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

 楼主| 发表于 2012-1-5 09:26:27 | 显示全部楼层 来自 中国–江苏–南京
效果二(规则运动):
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Puzzle effect - by Wander</title>
  6. <style type="text/css">
  7. * {margin:0; padding:0}
  8. ul {list-style:none}
  9. img {border:0}
  10. body {position:relative; background:white; text-align:center; font:12px/120% Arial,simsun,sans-serif; color:white; }
  11. .imgWrap {margin:0 auto}
  12. #thumbs {position:absolute; top: 600px; left:300px; width:400px}
  13. #thumbs li {float:left; margin:0 5px; cursor:pointer}
  14. #thumbs li img {float:left}
  15. #thumbs li.current {margin:-3px 2px 0 2px; padding:1px; border:2px solid black}
  16. </style>
  17. <script type="text/javascript">
  18. //code by Wander @ cnwander.com
  19. var xNum = 3,
  20. yNum = 3,
  21. xLimit,
  22. yLimit;;
  23. var thumbs,wrap;
  24. window.onload = function() {
  25. var thumbsWrap = $("#thumbs");
  26. setStyle(thumbsWrap,"left",($("body").clientWidth - thumbsWrap.offsetWidth)/2+"px");
  27. xLimit = [0,$("body").clientWidth];
  28. yLimit = [0,screen.height - 250];
  29. thumbs = $("#thumbs li");
  30. loadImg(thumbs[0].getAttribute("url"));
  31. each(thumbs,thumbEvts);
  32. }
  33. function thumbEvts() {
  34. this.onclick = thumbDown;
  35. function thumbDown(e) {
  36.   each(thumbs,function(){
  37.    this.className = "";
  38.   });
  39.   this.className = "current";
  40.   loadImg(this.getAttribute("url"));
  41. }
  42. }
  43. function loadImg(url) {
  44. var img = new Image();
  45. var left,
  46.   top = 100,
  47.   imgW,imgH;
  48. if(wrap) removeElem(wrap);
  49. wrap = addDiv($("body"),{className:"imgWrap"});
  50. img.src = url;
  51. wrap.innerHTML = "<img src="/articleimg/2009/11/7205/loading.gif" style="margin-top:250px"/>";
  52. if(img.width > 0) initImg();
  53. else img.onload = initImg;

  54. function initImg() {
  55.   imgW = img.width;
  56.   imgH = img.height;
  57.   left = ($("body").clientWidth - imgW)/2;
  58.   wrap.innerHTML = "";
  59.   numTimer = 0;
  60.   for(var i=0; i<xNum; i++) {
  61.    for(var j=0; j<yNum; j++) {
  62.     var xStart = (xLimit[1])*i/(xNum-1) - imgW*0.5/xNum,
  63.      yStart = (yLimit[1])*j/(yNum-1) - imgH/yNum;
  64.     addPiece(xStart,yStart,i,j);
  65.    }
  66.   }
  67. }

  68. function addPiece(xStart,yStart,xEnd,yEnd) {
  69.   var obj = addDiv(wrap),
  70.    w = imgW/xNum,
  71.    h = imgH/yNum,
  72.    opa = 0,
  73.    xPos = xEnd*w,
  74.    yPos = yEnd*h;   
  75.   
  76.   slideTo();
  77.   
  78.   function slideTo() {
  79.    var a = 0.2,
  80.     toScale = 1,
  81.     toX = left+xPos,
  82.     toY = top+yPos,
  83.     toOpa = 1,
  84.     timer = window.setInterval(move,20);
  85.    
  86.    numTimer++;
  87.    function move() {
  88.     xStart += (toX - xStart)*a;
  89.     yStart += (toY - yStart)*a;
  90.     opa += (toOpa - opa)*a;
  91.     if(Math.sqrt(Math.pow(toX-xStart,2)+Math.pow(toY-yStart,2))<=1) {
  92.      xStart = toX;
  93.      yStart = toY;
  94.      opa = toOpa;
  95.      window.clearInterval(timer);
  96.      numTimer--;
  97.      if(numTimer <= 0) {
  98.       setStyle(wrap,"paddingTop",top+"px");
  99.       wrap.innerHTML = "<img src=""+url+""/>";
  100.      }
  101.     }

  102.     obj.style.cssText = "position:absolute;left:"+xStart+"px;top:
  103. "+yStart+"px;width:"+w+"px;height:"+h+"px;background:url("+url+")
  104. no-repeat -"+xPos+"px -"+yPos+"px;filter:alpha(opacity="+ opa*100 +");
  105. -moz-opacity:"+ opa +"; opacity:"+ opa;
  106.    
  107.    }
  108.   }
  109.   
  110. }
  111. }
  112. function addDiv(where,attr) {
  113. var obj = document.createElement("div");
  114. if(attr)
  115. for(var key in attr) {
  116.   if(key == "className") obj.className = attr[key];
  117.   else obj.setAttribute(key,attr[key]);
  118. }
  119. where.appendChild(obj);
  120. return obj;
  121. }
  122. function removeElem(elem){
  123. var parent = elem.parentNode;
  124. if(parent){
  125.   parent.removeChild(elem);
  126. }
  127. }
  128. //遍历对象
  129. function each(obj,fuc) {
  130. for(var key in obj) {
  131.   obj[key].index = key;
  132.   fuc.call(obj[key]);  
  133. }  
  134. }
  135. function randowNum(start,end) {
  136. return Math.floor(Math.random()*(end - start)) + start;
  137. }
  138. //设置样式
  139. function setStyle() {
  140. if(arguments.length == 2 &&  typeof arguments[1] == "object") {
  141.   for(var key in arguments[1]) {
  142.    arguments[0].style[key] = arguments[1][key];
  143.   }
  144. } else if (arguments.length > 2) {
  145.   arguments[0].style[arguments[1]] = arguments[2];
  146. }
  147. }
  148. //elements finder
  149. function $(s,wrap) {
  150. var eleExpr =  /([a-zA-Z0-9]*)([#\.]?)(\w+)[^\s+]*/g,
  151.   result = [wrap || document];
  152. while(eleExpr.test(s)) {
  153.   var tag = RegExp.$1,
  154.    type = RegExp.$2,
  155.    content = RegExp.$3,
  156.    elems = [];
  157.   for(var i=0; i<result.length; i++) {
  158.    var temp;
  159.    switch(type) {
  160.     case "#":
  161.      temp = [result[i].getElementById(content)];
  162.      break;
  163.     case ".":
  164.      temp = getElemsByClassName(content,result[i],tag);
  165.      break;
  166.     default:
  167.      temp = result[i].getElementsByTagName(tag+content);
  168.    }
  169.    for(var key in temp) {
  170.     if(temp[key].nodeType == 1) elems.push(temp[key]);
  171.    }     
  172.   }
  173.   result = [];
  174.   for(var key in elems) {
  175.    if(elems[key].nodeType == 1) result.push(elems[key]);
  176.   }
  177. }
  178. return result.length == 1 ? result[0] : result;
  179. function getElemsByClassName(classname,elem,tag) {
  180.   var result = [],
  181.    tag = tag || "*",
  182.    allElems = elem.getElementsByTagName(tag) || elem.all;
  183.   for(var i=0; i<allElems.length; i++){
  184.    var list = allElems[i].className.split(" ");
  185.    for(var j=0; j<list.length; j++){
  186.     if(list[j] == classname) result.push(allElems[i]);
  187.    }      
  188.   }
  189.   return result;
  190. }
  191. }
  192. </script>
  193. </head>
  194. <body>
  195. <div
  196. style="position:absolute; right:0"><a
  197. href="http://cnwander.com/blog/?p=13">Wander's
  198. space</a></div>
  199. <ul id="thumbs">
  200. <li
  201. url="/articleimg/2009/11/7205/photo01.jpg" class="current"><img
  202. src="/articleimg/2009/11/7205/thumb01.jpg" alt=""/></li>
  203. <li url="/articleimg/2009/11/7205/photo02.jpg"><img src="/articleimg/2009/11/7205/thumb02.jpg" alt=""/></li>

  204.     <li url="/articleimg/2009/11/7205/photo03.jpg"><img
  205. src="/articleimg/2009/11/7205/thumb03.jpg" alt=""/></li>
  206. </ul>
  207. </body>
  208. </html>
复制代码

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

回复 支持 反对

使用道具 举报

发表于 2013-3-20 14:07:32 | 显示全部楼层 来自 中国–广东–湛江
为何总是复制别人的呢?楼主

点评

不复制别人搞不定啊~嘿嘿  详情 回复 发表于 2013-3-20 14:09

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-3-20 14:09:46 | 显示全部楼层 来自 中国–江苏–南京
光明使者 发表于 2013-3-20 14:07
为何总是复制别人的呢?楼主

不复制别人搞不定啊~嘿嘿

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

回复 支持 反对

使用道具 举报

发表于 2013-3-20 14:10:54 | 显示全部楼层 来自 中国–广东–湛江
mseed 发表于 2013-3-20 14:09
不复制别人搞不定啊~嘿嘿

呵呵~~~~连别人的连接也一起了

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-12-22 20:10 , Processed in 0.082337 second(s), 21 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表