Full function JS Slide Library

From , 5 Years ago, written in JavaScript, viewed 221 times.
URL https://pastebin.vip/view/96f0a190
  1. /**
  2.  * i use closure to simulate private method in OOP,because i don't wanna disturb the others js libraris you may use like jQuery,which uses $
  3.  * remember the only variable i inject to window is Slider
  4.  *
  5.  * (c)logan liu
  6.  * Email:hellouniverse@qq.com
  7.  * if you find bugs,Don't hesitate contacting me.
  8.  */
  9. (function(window){
  10.  
  11.     function Slider(contentID, handlerID, pageNum, onePageWidth, direction, step, speed){
  12.         if ((!contentID) || (!handlerID) || (!pageNum) || (!onePageWidth)) {
  13.             alert("init error,see your paraments");
  14.         }
  15.         this.contentID = contentID;
  16.         this.handlerID = handlerID;
  17.         this.pageNum = pageNum;
  18.         this.onePageWidth = onePageWidth;
  19.         this.step = step || 10;
  20.         this.direction = direction;
  21.         this.speed = speed || 1;
  22.     }
  23.     var $ = function(id){
  24.         return document.getElementById(id);
  25.     }
  26.    
  27.     var style = function(sid, key, value){
  28.         if (value)
  29.             $(sid).style[key] = value;
  30.         else
  31.             return $(sid).style[key];
  32.     };
  33.     /**
  34.      *
  35.      * @param {Object} id
  36.      * @param {Object} i
  37.      *  0-100
  38.      */
  39.     var fade = function(id, i){
  40.         style(id, "filter", "alpha(opacity=" + i + ")");
  41.         i = i / 100;
  42.         style(id, "-moz-opacity", i);
  43.         style(id, "-khtml-opacity", i);
  44.         style(id, "opacity", i);
  45.     };
  46.    
  47.     var log = function(){
  48.         if (!window.console)
  49.             return;
  50.         // console.log(arguments.callee.caller.toString());
  51.         for (var i in arguments) {
  52.             console.log(i + ":" + arguments[i]);
  53.         }
  54.        
  55.     }
  56.     var addEvent = function(elm, evType, fn, useCapture){
  57.         if (elm.addEventListener) {
  58.             elm.addEventListener(evType, fn, useCapture);
  59.         }
  60.         else
  61.             if (elm.attachEvent) {
  62.                 elm.attachEvent('on' + evType, fn);
  63.             }
  64.             else {
  65.                 elm['on' + evType] = fn;
  66.             }
  67.     };
  68.     var dump = function(o){
  69.         for (var i in o) {
  70.             log(i + ":" + o[i]);
  71.         }
  72.     };
  73.    
  74.     Slider.prototype = {
  75.         changeEvent: "onclick",
  76.         leftAndRightArrow: false,
  77.         pageHandler: true,
  78.         currentPage: 0,
  79.         transformEffect: "",
  80.         intervalId: 0,
  81.         duration: 6000,
  82.         /**
  83.          * anything can case to false stands for horizontal.and the others are vertical.
  84.          */
  85.         direction: 0,
  86.         /**
  87.          * rend page navigator like 1,2...,so you can override it with you method such as page1,page2..or just empty.
  88.          * @param {Object} i
  89.          */
  90.         rendPageNav: function(i){
  91.             return i == -1 || i == this.pageNum ? "" : (i + 1);
  92.         },
  93.         /**
  94.          * set navigator's class when clicked.add "current" to this.currentPage
  95.          * @param {Object} i
  96.          */
  97.         rendNav: function(j){
  98.             var c = $(this.handlerID).children;
  99.             j = this.leftAndRightArrow ? (j + 1) : j;
  100.             for (var i = (this.leftAndRightArrow ? 1 : 0), l = (this.leftAndRightArrow ? c.length - 1 : c.length); i < l; i++) {
  101.                 c[i].className = (i == j ? "current" : "");
  102.             }
  103.         },
  104.         initHandler: function(){
  105.             var tempThis = this;
  106.             for (var i = (this.leftAndRightArrow ? -1 : 0), len = (this.leftAndRightArrow ? this.pageNum + 1 : this.pageNum); i < len; i++) {
  107.                 if ((!tempThis.pageHandler) && i != -1 && i != tempThis.pageNum)
  108.                     continue;
  109.                 var a = document.createElement("a");
  110.                 a.href = "#nogo";
  111.                 a.className = (i == -1 ? "left" : (i == 0 ? "current" : (i == this.pageNum ? "right" : "")));
  112.                
  113.                 /**
  114.                  * must use closure here to keep i.
  115.                  * inspired by:Listing 2-16,page 29,<Pro JavaScript Techniques>,John Resig
  116.                  */
  117.                 (function(){
  118.                     var tempI = i;
  119.                     a[tempThis.changeEvent] = function(){
  120.                         this.blur();
  121.                         tempThis.toPage(tempI == -1 ? "-1" : (tempI == tempThis.pageNum ? "+1" : tempI));
  122.                     };
  123.                 })();
  124.                 a.innerHTML = tempThis.rendPageNav(i);
  125.                 $(this.handlerID).appendChild(a);
  126.             }
  127.         },
  128.         toPage: function(toNum){
  129.             if (toNum === "+1")
  130.                 toNum = this.currentPage + 1;
  131.             if (toNum === "-1")
  132.                 toNum = this.currentPage - 1;
  133.            
  134.             if (this.currentPage == toNum)
  135.                 return;
  136.             if (toNum >= this.pageNum)
  137.                 toNum = toNum % this.pageNum;
  138.             if (toNum < 0)
  139.                 toNum = this.pageNum - 1;
  140.             this.rendNav(toNum);
  141.            
  142.             var toPos = -toNum * this.onePageWidth;
  143.             var currentPos = -this.currentPage * this.onePageWidth;
  144.             this.stopAutoSwitch();
  145.             var value = this.direction ? "top" : "left";
  146.             this.transformFn(this.contentID, value, currentPos, toPos, this.startAutoSwitch);
  147.             //fix some pix
  148.             if (style(this.contentID, value) != toPos)
  149.                 style(this.contentID, value, toPos + "px");
  150.             this.currentPage = toNum;
  151.         },
  152.         /**
  153.          * all the transform funcitons must set value(left,top) from x to y.
  154.          * i don't care how you transfrom but the start stat and the end stat.
  155.          * @param {Object} id
  156.          * @param {Object} value
  157.          * @param {Object} from
  158.          * @param {Object} to
  159.          */
  160.         transformFn: function(id, value, from, to, callback){
  161.             //            log(this.transformEffect,id, value, from, to);
  162.             switch (this.transformEffect) {
  163.                 case "fade":
  164.                     var tempThis = this;
  165.                     //fade from 1 to 0 to 1,i change from 1 to 0 to -1
  166.                     (function fadeEffect(i){
  167.                         i = i - 5;
  168.                         if (i == -100) {
  169.                             fade(id, 100);
  170.                             callback.call(tempThis);
  171.                         }
  172.                         else {
  173.                             if (i == 0) {
  174.                                 style(id, value, to + "px");
  175.                             }
  176.                             fade(id, i >= 0 ? i : -i);
  177.                            
  178.                             window.setTimeout(function(){
  179.                                 fadeEffect(i);
  180.                             }, tempThis.speed);
  181.                         }
  182.                        
  183.                     })(100);
  184.                     break;
  185.                 case "slide":
  186.                     var neg = (to - from > 0) ? "1" : "-1";
  187.                     var tempThis = this;
  188.                     (function slideEffect(toTemp){
  189.                         style(tempThis.contentID, value, toTemp + "px");
  190.                         if ((to - toTemp) * neg > 0) {
  191.                             window.setTimeout(function(){
  192.                                 slideEffect(toTemp + neg * tempThis.step);
  193.                             }, tempThis.speed);
  194.                         }
  195.                         else {
  196.                             callback.call(tempThis);
  197.                         }
  198.                        
  199.                     })(from + neg * tempThis.step);
  200.                     break;
  201.                 default:
  202.                     style(id, value, to + "px");
  203.                     callback();
  204.                     break;
  205.             }
  206.            
  207.         },
  208.        
  209.         winonload: function(duration){
  210.             style(this.contentID, "position", "absolute");
  211.             this.initHandler();
  212.             this.startAutoSwitch();
  213.             return {};//it's necessary for IE6
  214.         },
  215.         startAutoSwitch: function(){
  216.             if (this.duration > 0) {
  217.                 var tempThis = this;
  218.                 this.intervalId = window.setInterval(function(){
  219.                     tempThis.toPage(tempThis.currentPage + 1)
  220.                 }, tempThis.duration);
  221.             }
  222.         },
  223.         stopAutoSwitch: function(){
  224.             if (this.intervalId)
  225.                 window.clearInterval(this.intervalId);
  226.         },
  227.         start: function(duration, transformEffect){
  228.             this.duration = duration || 3000;
  229.             this.transformEffect = transformEffect || "slide";
  230.             addEvent(window, 'load', this.winonload(), false);
  231.         }
  232.     };
  233.     window.Slider = Slider;
  234. })(window)
  235.  
  236.  
  237. //javascript/1082

Reply to "Full function JS Slide Library"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website