原生JS编写的照片墙效果实例演示特效

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>照片墙一多实例演示</title>
<style type="text/css">
body,div,h2,ul,li{margin:0;padding:0;}
body{font:12px/1.5 Arail;}
.box{width:860px;margin:10px auto;background:#eee;border:1px solid #b8b8b8;overflow:hidden}
.title{height:30px;line-height:30px;font-size:14px;padding:0 15px 0 35px;border-bottom:1px solid #b8b8b8;background:#fafafa url(http://js.alixixi.com/img/mm/ico.gif) 5px 50% no-repeat;}
.title span{float:left;}
.title a{float:right;color:#06f;outline:none;}
.title a:hover{color:red;}
.box ul{float:left;padding:0 15px 15px 0;}
.box li{float:left;width:140px;height:105px;padding:6px;background:#fff;border:1px solid #c3c3c3;display:inline;margin:15px 0 0 15px;list-style:none;}
.box li img{float:left;width:140px;height:105px;}
.box li.hig{padding:5px;border:2px dashed #f30;opacity:0.5;filter:alpha(opacity=50);}
</style>
<script type="text/javascript">
//获取ID
var $ = function (id) {return typeof id === "string" ? document.getElementById(id) : id};
//获取tagName
var $$ = function (tagName, oParent) {return (oParent || document).getElementsByTagName(tagName)};
//获取class
var $$$ = function (sClass, oParent) {
 var aClass = [],
 i = 0,
 reClass = new RegExp("(\\s|^)" + sClass + "($|\\s)"),
 aElement = $$("*", oParent);
 for (i = 0; i < aElement.length; i++)reClass.test(aElement[i].className) && aClass.push(aElement[i]);
 return aClass
};
//获取元素位置
function getPos(obj) {
 var iTop = obj.offsetTop;
 var iLeft = obj.offsetLeft;
 while (obj.offsetParent)
 {
  iTop += obj.offsetParent.offsetTop;
  iLeft += obj.offsetParent.offsetLeft;
  obj = obj.offsetParent;
 }
 return {top:iTop, left:iLeft}
};
//创建照片墙对象
var PhotoWall = function () {this.initialize.apply(this, arguments)};
PhotoWall.prototype = {
 initialize: function (obj, aData)
 {
  var oThis = this;
  this.oParent = $(obj);
  this.oUl = $$("ul", this.oParent)[0];
  this.oBtn = $$("a", this.oParent)[0];
  this.zIndex = 1;
  this.aPos = [];
  this.aData = aData;
  this.dom = document.documentElement || document.body;
  this.create();
  this.oBtn.onclick = function () {oThis.randomOrder()}
 },
 create: function ()
 {
  var aFrag = document.createDocumentFragment();
  var i = 0;
  for (i = 0; i < this.aData.length; i++)
  {
   var oLi = document.createElement("li");
   var oImg = document.createElement("img");
   oImg.src = this.aData[i];
   oLi.appendChild(oImg);
   aFrag.appendChild(oLi)
  }
  this.oUl.appendChild(aFrag);
  this.aLi = $$("li", this.oParent);
  this.changeLayout()
 },
 changeLayout: function ()
 {
  var i = 0;
  this.oParent.style.height = this.oParent.offsetHeight - 2 + "px";
  this.aPos.length = 0;
  for (i = 0; i < this.aLi.length; i++) this.aLi[i].style.cssText = "";
  for (i = 0; i < this.aLi.length; i++)
  {
   this.aLi[i].index = i;
   this.aLi[i].style.top = getPos(this.aLi[i]).top + "px";
   this.aLi[i].style.left = getPos(this.aLi[i]).left + "px";
   this.aPos.push({left:getPos(this.aLi[i]).left, top:getPos(this.aLi[i]).top})
  }
  for (i = 0; i < this.aLi.length; i++)
  {
   this.aLi[i].style.position = "absolute";
   this.aLi[i].style.margin = "0";
   this.drag(this.aLi[i])
  }
 },
 drag: function (obj, handle)
 {
  var oThis = this;
  var handle = handle || obj;
  handle.style.cursor = "move";
  handle.onmousedown = function (event)
  {
   var event = event || window.event;
   var disX = event.clientX - this.offsetLeft;
   var disY = event.clientY - this.offsetTop;
   var oNear = null;
   handle.style.zIndex = oThis.zIndex++;

   document.onmousemove = function (event)
   {
    var event = event || window.event;
    var iL = event.clientX - disX;
    var iT = event.clientY - disY;
    var maxL = Math.max(oThis.dom.clientWidth, oThis.dom.scrollWidth) - handle.offsetWidth;
    var maxT = Math.max(oThis.dom.clientHeight, oThis.dom.scrollHeight) - handle.offsetHeight;

    iL < 0 && (iL = 0);
    iT < 0 && (iT = 0);
    iL > maxL && (iL = maxL);
    iT > maxT && (iT = maxT);

    handle.style.left = iL + "px";
    handle.style.top = iT + "px";

    oNear = oThis.findNearest(obj);

    for (var i = 0; i < oThis.aLi.length; i++) oThis.aLi[i].className = "";

    oNear && (oNear.className = "hig");

    return false
   };
   document.onmouseup = function ()
   {
    document.onmousemove = null;
    document.onmouseup = null;

    if (oNear)
    {
     handle.index = [handle.index, oNear.index];
     oNear.index = handle.index[0];
     handle.index = handle.index[1];
     oNear.style.zIndex = oThis.zIndex++;
     oThis.doMove(handle, oThis.aPos[handle.index]);
     oThis.doMove(oNear, oThis.aPos[oNear.index]);
     oNear.className = "";
    }
    else
    {
     oThis.doMove(handle, oThis.aPos[handle.index])
    }

    handle.releaseCapture && handle.releaseCapture()
   };
   this.setCapture && this.setCapture();
   return false
  };
 },
 doMove: function (obj, iTarget, callback)
 {
  var oThis = this;
  clearInterval(obj.timer);
  obj.timer = setInterval(function ()
  {
   var iCurL = getPos(obj).left;
   var iCurT = getPos(obj).top;
   var iSpeedL = (iTarget.left - iCurL) / 5;
   var iSpeedT = (iTarget.top - iCurT) / 5;
   iSpeedL = iSpeedL > 0 ? Math.ceil(iSpeedL) : Math.floor(iSpeedL);
   iSpeedT = iSpeedT > 0 ? Math.ceil(iSpeedT) : Math.floor(iSpeedT);

   if (iCurL == iTarget.left && iCurT == iTarget.top)
   {
    clearInterval(obj.timer);
    callback && callback()
   }
   else
   {
    obj.style.left = iCurL + iSpeedL + "px";
    obj.style.top = iCurT + iSpeedT + "px"
   }
  }, 30)
 },
 findNearest: function (obj)
 {
  var aDistance = [];
  var i = 0;
  for (i = 0; i < this.aLi.length; i++) aDistance[i] = this.aLi[i] == obj ? Number.MAX_VALUE : this.getDistance(obj, this.aLi[i]);

  var minNum = Number.MAX_VALUE;
  var minIndex = -1;
  for (i = 0; i < aDistance.length; i++) aDistance[i] < minNum && (minNum = aDistance[i], minIndex = i);

  return this.isButt(obj, this.aLi[minIndex]) ? this.aLi[minIndex] : null
 },
 getDistance: function(obj1, obj2)
 {
  var a = (obj1.offsetLeft + obj1.offsetWidth / 2) - (obj2.offsetLeft + obj2.offsetWidth / 2);
  var b = (obj1.offsetTop + obj1.offsetTop / 2) - (obj2.offsetTop + obj2.offsetTop / 2);
  return Math.sqrt(a * a + b * b)
 },
 isButt: function (obj1, obj2)
 {
  var l1 = obj1.offsetLeft;
  var t1 = obj1.offsetTop;
  var r1 = l1 + obj1.offsetWidth;
  var b1 = t1 + obj1.offsetHeight;

  var l2 = obj2.offsetLeft;
  var t2 = obj2.offsetTop;
  var r2 = l2 + obj2.offsetWidth;
  var b2 = t2 + obj2.offsetHeight;

  return !(r1 < l2 || b1 < t2 || r2 < l1 || b2 < t1)
 },
 randomOrder: function ()
 {
  this.aPos.sort(function () {return Math.random() > 0.5 ? 1 : -1});
  for (var i = 0; i < this.aLi.length; i++)
  {
   this.aLi[i].index = i;
   this.doMove(this.aLi[i], this.aPos[i])
  }
 }
};
window.onload = function ()
{
 var aBox = $$$("box");
 var aData = [];
 var aExample = [];
 var i = 0;
 //生成图片数据
 for (i = 0; i < 20; i++) aData[aData.length] = "http://js.alixixi.com/img/mm/" + i + ".jpg";
 //循环创建多个实例
 for (i = 0; i < aBox.length; i++)
 {
  var oExample = new PhotoWall(aBox[i], aData);
  aExample.push(oExample)
 }
 this.onresize = function ()
 {
  for (var p in aExample) aExample[p].changeLayout()
 };
 this.onresize()
};
</script>
</head>
<body>
<div class="box">
    <h2 class="title"><span>一堆90后</span><a href="javascript:;" class="order">随机排序</a></h2>
    <ul></ul>
</div>
<div class="box">
    <h2 class="title"><span>一堆90后</span><a href="javascript:;" class="order">随机排序</a></h2>
    <ul></ul>
</div>
</body>
</html>

效果:

时间: 2024-09-25 08:52:05

原生JS编写的照片墙效果实例演示特效的相关文章

原生JS实现的放大镜效果实例代码_javascript技巧

这是我用原生js写的放大镜效果,与各种各样的框架技术相比,我喜欢使用原生的js,在这里,想和大家一起谈谈原生和框架技术的理解与个人喜好. <!DOCTYPE HTML> <html> <head> <title>js放大镜效果</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <style

原生js编写焦点图效果_javascript技巧

自己用原生js写的简单焦点图,算是对原生js初步运用. html部分 <div class="focusPicture"> <section class="mPhoto" id="mPhoto"> <img src="images/20161021/b_5809b93b66d18.jpg"/> </section><!-- 主要的大图 --> <section

原生JS实现轮播效果+学前端的感受(防止走火入魔)_javascript技巧

插件!插件!天天听到有人求这个插件,那个插件的,当然,用第三方插件可以大幅提高开发效率,但作为新手,我还是喜欢自己来实现,主要是我有时间! 今天我来给大家分享下用原生JS实现图片轮播的写法 前辈们可以无视下面这段废话: 在开始之前,先说下我学前端到现在的一点感受.到今天应该有两个月左右了吧,基本每天6-10小时的学习时间,纯自学,据说培训不靠谱!本人目前的阶段是只会三大件(HTML5.CSS3.javascript),其它所有知识都还排在学习计划后面....现在正处在迷茫期,不知道下面该先学什么

原生js代码实现秒表效果

jQuery用的多了,逐渐忘记了原生js如何书写代码.今天看到一个面试题,利用原生js写一个计算鼠标移入某个区域所停留的时长,移出暂停,再次移入累加. 于是想到了,利用原生js实现一个秒表效果.做之前也搜索了一些秒表效果,是利用setTimeout()实现的.本文是利用setInterval()定时器实现的. html代码如下: <div> <span id="date">00</span> - <span id="hour"

原生js编写设为首页兼容ie、火狐和谷歌_javascript技巧

原生 js 编写,兼容 ie,火狐和谷歌. 完整代码: <script type="text/javascript"> //设为首页 www.jb51.net function SetHome(obj,url){ try{ obj.style.behavior='url(#default#homepage)'; obj.setHomePage(url); }catch(e){ if(window.netscape){ try{ netscape.security.Privi

原生js和jQuery写的网页选项卡特效对比

  原生js和jQuery写的网页选项卡特效对比  总的来说思路比较简单,就是先获取节点,然后对节点进行相应的处理,下面是完整的页面代码: 原生js: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 6

JS实现的倒计时效果实例(2则实例)_javascript技巧

本文实例讲述了JS实现的倒计时效果.分享给大家供大家参考,具体如下: 我们经常会看到某些网站在注册的时候喜欢搞个按钮倒计时的效果,就是多少秒之后注册这个按钮才可以点击,其目的就是强迫你去看他的注册注意事项,这是一个很实用的效果:另外当我们进行在线考试的时候也必定会碰到答题倒计时的效果,这种倒计时效果是如何实现的呢?下面我们就用Js来实现一个倒计时效果,具体代码: html页面: <html> <head> <title>倒计时</title> <!--

原生js编写autoComplete插件_javascript技巧

最近有提关于下拉选项过多的时候,希望输入关键词,可以搜索内容的需求,但是之前项目太赶,所以也就没有来得及做,因为希望用原生js写一些内容,所以插件是采用了原生js写的思路如下第一步:fnInit实现初始化一些字段第二步:加载搜索框的div第三步:实现search功能,删除原节点并加载新节点第四步:点击或者回车的时候设置value 代码: autoComplete.js /** * @summary AutoComplete * @description 输入框自动检索下拉选项 * @versio

基于Turn.js 实现翻书效果实例解析_javascript技巧

最近项目经理我个项目练练手,其项目需求是要实现翻书效果,看到这个需求后,我真是懵了,这咋整,我可是java出身的啊,这个问题真是难住我了,后来有同事的指导,之前他曾经做过PC版的翻书效果,当时使用的是Turn.js ,查过其相关API后,整个人突然豁然开朗呀,使用Turn.js 完全可以解决当前我接手这个项目的所有需求呀.下面小编把我的学习心得分享给大家,大家可以参考下 Turn.js的官方网址: http://www.turnjs.com/ 下面是我这个项目上线后的效果: 看过实际项目后,各位