[JavaScript] the js code →→→→→进入此内容的聊天室

来自 , 2019-03-27, 写在 JavaScript, 查看 166 次.
URL http://www.code666.cn/view/352fe25d
  1. //this module's functionality is to create instances of slide,
  2. //different instances will be easy to control, because everyone has different scope
  3. //it's a good practise to write maintainable js code
  4. //please refer: https://www.youtube.com/watch?v=OzjogCFO4Zo
  5. var SlideModule = (function () {
  6.     var Slide = function (settings) {
  7.         this.btns = settings.btns;
  8.         this.pics = settings.pics;
  9.         this.picWidth = settings.picWidth;
  10.         this.container = settings.container;
  11.         //to set the default value
  12.         this.span = settings.span || 1000;
  13.         this.currentIndex = 0;
  14.  
  15.         //I call the init function here to make sure the instances work immediately after initialization
  16.         //but I can also let clients to call this method, if they want to control the behavior, it all depends.
  17.         this.Init();
  18.     }
  19.  
  20.     Slide.prototype.Init = function () {
  21.         $(this.btns[0]).css({ "background": "pink" })
  22.         //to change the context because of the closure
  23.         var that = this;
  24.         //to bind the callback functions according to the events
  25.         $.each(this.btns, function (i, btn) {
  26.             //to bind every button with the corresponding picture
  27.             $(btn).on("mouseover", function () {
  28.                 that.interval && clearInterval(that.interval);
  29.                 that.currentIndex = i;
  30.                 that.Run(that.currentIndex);
  31.             });
  32.             $(btn).on("mouseout", function () {
  33.                 that.GetNextIndex()
  34.             })
  35.         });
  36.         this.GetNextIndex();
  37.     }
  38.  
  39.     //this function is only responsible to find out the next index of pictures to show
  40.     Slide.prototype.GetNextIndex = function () {
  41.         //clear the interval if it exists
  42.         this.interval && clearInterval(this.interval);
  43.         //to change the context because of the closure
  44.         var that = this;
  45.         this.interval = setInterval(function () {
  46.             //to ensure the index should be smaller than the length of the buttons(the amount of the buttons and pictures are same)
  47.             that.currentIndex = ++that.currentIndex >= that.btns.length ? 0 : that.currentIndex;
  48.             //call the Run function of the current instance
  49.             that.Run(that.currentIndex);
  50.         }, this.span)
  51.     }
  52.  
  53.     //this function is the true one to set the layout of the slide.
  54.     Slide.prototype.Run = function (index) {
  55.         //to compute the right position every time
  56.         this.container.css({ "left": -1 * this.picWidth * index + "px" });
  57.         //to change the buttons' style
  58.         this.btns.css({"background":"white"})
  59.         $(this.btns[index]).css({"background":"pink"})
  60.     }
  61.  
  62.     return Slide;
  63. }())
  64.  
  65. //this class is to demostrate inheritance and polymorphism
  66. var DerivedSlideModule = (function(){
  67.         if(SlideModule.constructor === Function){
  68.                 var DerivedSlide = function(){
  69.                         SlideModule.apply(this, arguments)
  70.                 }
  71.                
  72.                 DerivedSlide.prototype = Object.create(SlideModule.prototype)
  73.                 //to override the same function of the base class
  74.                 DerivedSlide.prototype.GetNextIndex = function(){
  75.                         this.interval && clearInterval(this.interval);
  76.                         //to change the context because of the closure
  77.                         var that = this;
  78.                         this.interval = setInterval(function () {
  79.                                 //to set the every second picture's index
  80.                                 that.currentIndex+=2;
  81.                                 //to ensure the index should be smaller than the length of the buttons(the amount of the buttons and pictures are same)
  82.                                 that.currentIndex = that.currentIndex >= that.btns.length ? 0 : that.currentIndex;
  83.                             //call the Run function of the current instance
  84.                                 that.Run(that.currentIndex);
  85.                         }, this.span)
  86.                 };
  87.                 return DerivedSlide;
  88.         }
  89.         return;
  90. }())
  91.  
  92. //in order to write testable and maintainable js code,
  93. //I tried to apply OOP principles to JS
  94. //abstract the relative class by using function
  95. //encapsulate properties in constructor function and inheritable functions to prototype chain
  96. //for polymorphism, I overrided the specific function on the prototype chain.
  97. $(function () {
  98.     //to call this function, please make sure the amount of the buttons and pictures are same
  99.         var slideModule = new SlideModule({
  100.                 btns: $("#btns > input[type=button]"),
  101.                 pics: $("#container > img"),
  102.                 picWidth: 250,
  103.                 container: $("#container")
  104.         });
  105.        
  106.     //create an instance of the derived class
  107.         var slideModule1 = new DerivedSlideModule({
  108.                 btns: $("#btns1 > input[type=button]"),
  109.                 pics: $("#container1 > img"),
  110.                 picWidth: 250,
  111.                 container: $("#container1")
  112.         });
  113. })

回复 "the js code"

这儿你可以回复上面这条便签

captcha