龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

JS实现多图点击切换,鼠标移上放大(1)(2)

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
VVG.base库代码: /* *简单JS库封装ByVVG *@namespaceVVG *E-mail:mysheller@163.comQQ:83816819 */ if(!String.trim){ String.prototype.trim = function (){ var reg =/^\s+|\s+$/g; returnthis.replace(

VVG.base库代码:

  1. /*  
  2.  *    简单JS库封装  By VVG  
  3.  *    @namespace VVG  
  4.  *    E-mail:mysheller@163.com    QQ:83816819  
  5.  */  
  6. if (!String.trim) {  
  7.     String.prototype.trim = function () {  
  8.         var reg = /^\s+|\s+$/g;  
  9.         return this.replace(reg, '');  
  10.     }  
  11. }  
  12. (function () {  
  13.     // create namespace VVG  
  14.     if (!window.VVG) {  
  15.         window.VVG = {};  
  16.     }  
  17.  
  18.     function isCompatible(other) {  
  19.         // Use capability detection to check requirements  
  20.         if (other === false || !Array.prototype.push || !Object.hasOwnProperty || !document.createElement || !document.getElementsByTagName) {  
  21.             alert('你的浏览器不支持某些特性!');  
  22.             return false;  
  23.         }  
  24.         return true;  
  25.     }  
  26.  
  27.     window.VVG.isCompatible = isCompatible;  
  28.  
  29.  
  30.     // getElementById function  
  31.  
  32.     function $() {  
  33.         var elements = new Array();  
  34.         for (var i = 0; i < arguments.length; i++) {  
  35.             var element = arguments[i];  
  36.             if (typeof element == 'string') {  
  37.                 element = document.getElementById(element);  
  38.             }  
  39.             if (!element) {  
  40.                 continue;  
  41.             }  
  42.             // 如果只有一个参数,则返回  
  43.             if (arguments.length == 1) {  
  44.                 return element;  
  45.             }  
  46.             // 多个参数的时候存为数组  
  47.             elements.push(element);  
  48.         }  
  49.         // 返回数组  
  50.         return elements;  
  51.     }  
  52.  
  53.     window.VVG.$ = $;  
  54.  
  55.     // 获取Parent父元素下标签名为child 的 Tags  
  56.  
  57.     function $$(tag, parent) {  
  58.         parentparent = parent || document;  
  59.         return $(parent).getElementsByTagName(tag);  
  60.     }  
  61.  
  62.     window.VVG.$$ = $$;  
  63.  
  64.     // getElementsByClassName  
  65.  
  66.     function $$$(str, parent, tag) {  
  67.         //父节点存在  
  68.         if (parent) {  
  69.             parent = $(parent);  
  70.         } else {  
  71.             // 未传值时,父节点为body  
  72.             parent = document;  
  73.         }  
  74.         // tagContent为节点类型,未传值时为all节点  
  75.         tagtag = tag || '*';  
  76.         // 在父节点查找子节点,建立空数组arr  
  77.         var els = parent.getElementsByTagName(tag),  
  78.             arr = [];  
  79.         for (var i = 0n = els.length; i < n; i++) {  
  80.             // 查找每个节点下的classname,以空格分离为一个k数组  
  81.             for (var j = 0k = els[i].className.split(' '), l = k.length; j < 1; j++) {  
  82.                 // 当K数组中有一个值与str值相等时,记住这个标签并推入arr数组  
  83.                 if (k[j] == str) {  
  84.                     arr.push(els[i]);  
  85.                     break;  
  86.                 }  
  87.             }  
  88.         }  
  89.         // 返回数组  
  90.         return arr;  
  91.     }  
  92.  
  93.     window.VVG.$$$ = $$$;  
  94.     window.VVG.getElementsByClassName = $$$;  
  95.  
  96.     // Event事件绑定:绑定type事件到element元素,触发func函数  
  97.  
  98.     function bindEvent(element, type, func) {  
  99.         if (element.addEventListener) {  
  100.             element.addEventListener(type, func, false); //false 表示冒泡  
  101.         } else if (element.attachEvent) {  
  102.             element.attachEvent('on' + type, func);  
  103.         } else {  
  104.             element['on' + type] = func;  
  105.         }  
  106.     }  
  107.  
  108.     window.VVG.bindEvent = bindEvent;  
  109.  
  110.     // 解除Event事件绑定  
  111.  
  112.     function unbindEvent(element, type, func) {  
  113.         if (element.removeEventListener) {  
  114.             element.removeEventListener(type, func, false);  
  115.         } else if (element.detachEvent) {  
  116.             element.detachEvent('on' + type, func);  
  117.         } else {  
  118.             element['on' + type] = null;  
  119.         }  
  120.     }  
  121.  
  122.     window.VVG.unbindEvent = unbindEvent;  
  123.  
  124.     // 获取事件  
  125.  
  126.     function getEvent(event) {  
  127.         return event || window.event;  
  128.     }  
  129.  
  130.     window.VVG.getEvent = getEvent;  
  131.  
  132.     // 获取事件目标  
  133.  
  134.     function getTarget(event) {  
  135.         return event.target || event.srcElement;  
  136.     }  
  137.  
  138.     window.VVG.getTarget = getTarget;  
  139.  
  140.     // 获取鼠标位于文档的坐标  
  141.  
  142.     function getMousePositionInPage(event) {  
  143.         event = getEvent(event);  
  144.         return {  
  145.             'x':event.pageX || event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft),  
  146.             'y':event.pageY || event.clientY + (document.documentElement.scrollTop || document.body.scrollTop)  
  147.         }  
  148.     }  
  149.  
  150.     window.VVG.getMousePositionInPage = getMousePositionInPage;  
  151.  
  152.     // 停止事件冒泡  
  153.  
  154.     function stopPropagation(event) {  
  155.         if (event.stopPropagation) {  
  156.             event.stopPropagation();  
  157.         } else {  
  158.             event.cancelBubble = true;  
  159.         }  
  160.     }  
  161.  
  162.     window.VVG.stopPropagation = stopPropagation;  
  163.  
  164.     // 阻止默认事件  
  165.  
  166.     function preventDefault(event) {  
  167.         if (event.preventDefault) {  
  168.             event.preventDefault();  
  169.         } else {  
  170.             event.returnValue = false;  
  171.         }  
  172.     }  
  173.  
  174.     window.VVG.preventDefault = preventDefault;  
  175.  
  176.     //  apply从新定义函数的执行环境  
  177.  
  178.     function bindFunction(obj, func) {  
  179.         return function () {  
  180.             return func.apply(obj, arguments);  
  181.         };  
  182.     }  
  183.  
  184.     window.VVG.bindFunction = bindFunction;  
  185.  
  186.     // 设置透明度  
  187.  
  188.     function setOpacity(node, level) {  
  189.         node = $(node);  
  190.         if (document.all) {  
  191.             node.style.filter = 'alpha(opacity=' + level + ')';  
  192.         } else {  
  193.             node.style.opacity = level / 100;  
  194.         }  
  195.     }  
  196.  
  197.     window.VVG.setOpacity = setOpacity;  
  198.  
  199.     // 获取可视窗口尺寸  
  200.  
  201.     function getWindowSize() {  
  202.         var de = document.documentElement;  
  203.         return {  
  204.             'width':(  
  205.                 window.innerWidth || (de && de.clientWidth) || document.body.clientWidth),  
  206.             'height':(  
  207.                 window.innerHeight || (de && de.clientHeight) || document.body.clientHeight)  
  208.         }  
  209.     }  
  210.  
  211.     window.VVG.getWindowSize = getWindowSize;  
  212.  
  213.     //  数字转化为千分位格式函数  
  214.  
  215.     function thousandsNumberFormat(str) {  
  216.         var n = str.length;  
  217.         var c = n % 3;  
  218.         var reg = /\d{3}(?!$)/g;  
  219.         if (n > 3) {  
  220.             var strstr1 = str.slice(0, c);  
  221.             var strstr2 = str.slice(c, n);  
  222.             str1str1 = str1 ? str1 + ',' : '';  
  223.             str = str1 + str2.replace(reg, function (p1) {  
  224.                 return p1 + ',';  
  225.             })  
  226.         }  
  227.         return str;  
  228.     }  
  229.  
  230.     window.VVG.thousandsNumberFormat = thousandsNumberFormat;  
  231.  
  232.     // 带横杠的字符形式转化为驼峰式命名  
  233.  
  234.     function camelize(string) {  
  235.         return string.replace(/-(\w)/g, function (strMatch, p1) {  
  236.             return p1.toUpperCase();  
  237.         });  
  238.     }  
  239.  
  240.     window.VVG.camelize = camelize;  
  241.  
  242.     // 驼峰式转化为横杠分隔  
  243.  
  244.     function uncamelize(string, sep) {  
  245.         sepsep = sep || '-';  
  246.         return string.replace(/([a-z])([A-Z])/g, function (strMatch, p1, p2) {  
  247.             return p1 + sep + p2.toLowerCase();  
  248.         });  
  249.     }  
  250.  
  251.     window.VVG.uncamelize = uncamelize;  
  252.  
  253.     // 设置根据ID设置样式  
  254.  
  255.     function setStyleById(element, cssjson) {  
  256.         element = $(element);  
  257.         for (property in cssjson) {  
  258.             if (!cssjson.hasOwnProperty(property)) continue;  
  259.             if (property == 'opacity') {  
  260.                 setOpacity(element, cssjson[property]);  
  261.             } else {  
  262.                 element.style[camelize(property)] = cssjson[property];  
  263.             }  
  264.         }  
  265.  
  266.     }  
  267.  
  268.     window.VVG.setStyleById = setStyleById;  
  269.     window.VVG.setStyle = setStyleById;  
  270.  
  271.     // 根据Class类设置样式  
  272.  
  273.     function setStyleByClassName(classname, cssjson, parent, tag) {  
  274.         var elements = $$$(classname, parent, tag);  
  275.         for (i = 0n = elements.length; i < n; i++) {  
  276.             setStyleById(elements[i], cssjson);  
  277.         }  
  278.     }  
  279.  
  280.     window.VVG.setStyleByClassName = setStyleByClassName;  
  281.  
  282.     // 根据HTML标签TAG设置样式  
  283.  
  284.     function setStyleByTagName(tag, cssjson, parent) {  
  285.         var tags = $$(tag, parent);  
  286.         for (var i = 0; i < tags.length; i++) {  
  287.             setStyleById(tags[i], cssjson);  
  288.         }  
  289.     }  
  290.  
  291.     window.VVG.setStyleByTagName = setStyleByTagName;  
  292.  
  293.     // 获取Element元素的className  
  294.  
  295.     function getClassNames(element) {  
  296.         if (!(element = $(element))) return false;  
  297.         return element.className.replace(/\s+/g, ' ').split(' ');  
  298.  
  299.  
  300.     }  
  301.  
  302.     window.VVG.getClassNames = getClassNames;  
  303.  
  304.     // 查找element元素是否含有class  
  305.  
  306.     function hasClassName(element, classname) {  
  307.         if (!(element = $(element))) return false;  
  308.         var classNames = getClassNames(element);  
  309.         for (var i = 0; i < classNames.length; i++) {  
  310.             if (classNames[i] === classname) return true;  
  311.         }  
  312.         return false;  
  313.     }  
  314.  
  315.     window.VVG.hasClassName = hasClassName;  
  316.  
  317.     // 增加class  
  318.  
  319.     function addClassName(element, classname) {  
  320.         if (!(element = $(element))) return false;  
  321.         element.className += (element.className ? ' ' : '') + classname;  
  322.     }  
  323.  
  324.     window.VVG.addClassName = addClassName;  
  325.  
  326.     // 删除其中一个className  
  327.  
  328.     function removeClassName(element, classname) {  
  329.         if (!(element = $(element))) return false;  
  330.         if (hasClassName(element, classname)) {  
  331.             var classtexts = getClassNames(element);  
  332.             for (var i = 0; i < classtexts.length; i++) {  
  333.                 if (classtexts[i] == classname) {  
  334.                     delete(classtexts[i]);  
  335.                 }  
  336.             }  
  337.             element.className = classtexts.join(' ');  
  338.         }  
  339.  
  340.     }  
  341.  
  342.     window.VVG.removeClassName = removeClassName;  
  343.  
  344.     // 增加一个外部链接CSS  
  345.  
  346.     function addStyleSheet(url, media) {  
  347.         mediamedia = media || 'screen';  
  348.         var link = document.createElement('LINK');  
  349.         link.setAttribute('rel', 'stylesheet');  
  350.         link.setAttribute('type', 'text/css');  
  351.         link.setAttribute('href', url);  
  352.         link.setAttribute('media', media);  
  353.         document.getElementsByTagName('head')[0].appendChild(link);  
  354.     }  
  355.  
  356.     window.VVG.addStyleSheet = addStyleSheet;  
  357.  
  358.     // 删除一个外部链接CSS  
  359.  
  360.     function removeStyleSheet(url) {  
  361.         var stylesheets = document.getElementsByTagName('link');  
  362.         for (var i = 0; i < stylesheets.length; i++) {  
  363.             if (!(stylesheets[i].href.indexOf(url) == -1)) {  
  364.                 stylesheets[i].parentNode.removeChild(stylesheets[i]);  
  365.                 return true;  
  366.             }  
  367.         }  
  368.         return false;  
  369.     }  
  370.  
  371.     window.VVG.removeStyleSheet = removeStyleSheet;  
  372.  
  373.     // COOKIE 操作  
  374.  
  375.     function getCookie(name) {  
  376.         var start = document.cookie.indexOf(name + "=");  
  377.         var len = start + name.length + 1;  
  378.         if ((!start) && (name != document.cookie.substring(0, name.length))) {  
  379.             return null;  
  380.         }  
  381.         if (start == -1) return null;  
  382.         var end = document.cookie.indexOf(";", len);  
  383.         if (end == -1) end = document.cookie.length;  
  384.         return unescape(document.cookie.substring(len, end));  
  385.     }  
  386.  
  387.     window.VVG.getCookie = getCookie;  
  388.  
  389.     function setCookie(name, value, expires, path, domain, secure) {  
  390.         var today = new Date();  
  391.         today.setTime(today.getTime());  
  392.         if (expires) {  
  393.             expiresexpires = expires * 1000 * 60 * 60 * 24;  
  394.         }  
  395.         var expires_date = new Date(today.getTime() + (expires));  
  396.         document.cookie = name + "=" + escape(value) + ((expires) ? ";expires=" + expires_date.toGMTString() : "") + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ((secure) ? ";secure" : "");  
  397.     }  
  398.  
  399.     window.VVG.setCookie = setCookie;  
  400.  
  401.     function deleteCookie(name, path, domain) {  
  402.         if (getCookie(name)) document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";  
  403.     }  
  404.  
  405.     window.VVG.deleteCookie = deleteCookie;  
  406.  
  407.     // ajax对象操作  
  408.     // 安全过滤JSON的函数parseJSON  
  409.  
  410.     function parseJSON(s, filter) {  
  411.         var j;  
  412.  
  413.         function walk(k, v) {  
  414.             var i;  
  415.             if (v && typeof v === 'object') {  
  416.                 for (i in v) {  
  417.                     if (v.hasOwnProperty(i)) {  
  418.                         v[i] = walk(i, v[i]);  
  419.                     }  
  420.                 }  
  421.             }  
  422.             return filter(k, v);  
  423.         }  
  424.  
  425.         if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(s)) {  
  426.             try {  
  427.                 j = eval('(' + s + ')');  
  428.             } catch (e) {  
  429.                 throw new SyntaxError("parseJSON");  
  430.             }  
  431.         } else {  
  432.             throw new SyntaxError("parseJSON");  
  433.         }  
  434.         if (typeof filter === 'function') {  
  435.             j = walk('', j);  
  436.         }  
  437.         return j;  
  438.     }  
  439.  
  440.     // 创建一个XMLHttpRequest对象  
  441.  
  442.     function getRequestObject(url, options) {  
  443.         var req = false;  
  444.         if (window.XMLHttpRequest) {  
  445.             req = new window.XMLHttpRequest();  
  446.         } else if (window.ActiveXObject) {  
  447.             req = new window.ActiveXObject('Microsoft.XMLHTTP');  
  448.         }  
  449.         if (!req) return false;  
  450.         // 设置默认数据  
  451.         optionsoptions = options || {};  
  452.         optionsoptions.method = options.method || 'GET';  
  453.         optionsoptions.send = options.send || null;  
  454.         // 定义事件侦听函数  
  455.         req.onreadystatechange = function () {  
  456.             switch (req.readyState) {  
  457.                 case 1:  
  458.                     // 正在载入  
  459.                     if (options.loadListener) {  
  460.                         options.loadListener.apply(req, arguments);  
  461.                     }  
  462.                     break;  
  463.                 case 2:  
  464.                     // 载入完成  
  465.                     if (options.loadedListener) {  
  466.                         options.loadedListener.apply(req, arguments);  
  467.                     }  
  468.                     break;  
  469.                 case 3:  
  470.                     // 正在交互  
  471.                     if (options.ineractiveListener) {  
  472.                         options.ineractiveListener.apply(req, arguments);  
  473.                     }  
  474.                     break;  
  475.                 case 4:  
  476.                     // 交互完成  
  477.                     try {  
  478.                         if (req.status && req.status == 200) {  
  479.                             // 获取文件格式  
  480.                             // 为不同的content-type设置对应的方法  
  481.                             var contentType = req.getResponseHeader('Content-Type');  
  482.                             var mimeType = contentType.match(/\s*([^;]+)\s*(;|$)/i)[1];  
  483.                             switch (mimeType) {  
  484.                                 case 'text/plain':  
  485.                                     if (options.txtResponseListener) {  
  486.                                         options.txtResponseListener.call(req, req.responseText);  
  487.                                     }  
  488.                                     break;  
  489.                                 case 'text/javascript':  
  490.                                 case 'application/javascript':  
  491.                                     if (options.jsResponseListener) {  
  492.                                         options.jsResponseListener.call(req, req.responseText);  
  493.                                     }  
  494.                                     break;  
  495.                                 case 'application/json':  
  496.                                     if (options.jsonResponseListener) {  
  497.                                         try {  
  498.             &nbs
精彩图集

赞助商链接