Django打造大型企業官網(六)


4.9.根據輪播圖個數修改小圓點數量

src/js/index.js

function Banner() {
    this.bannerWidth = 798;
}

Banner.prototype.initBanner = function () {
    var self = this;
    this.bannerUL.css({
        "width":self.bannerWidth*self.bannerCount
    })
};

Banner.prototype.initPageControl = function () {
  var self = this;
  var pageControl = $(".page-control");
  for (var i=0;i<self.bannerCount;i++){
      var circle = $("<li></li>");
      pageControl.append(circle);
      if (i === 0){
          circle.addClass("active");
      }
  }
  pageControl.css({"width":self.bannerCount*12+8*2+16*(self.bannerCount-1)});
};

Banner.prototype.run = function () {
    this.initBanner();
    this.initPageControl();
    this.loop();
    this.listenArrowClick();
};

4.10.小圓點點擊事件和自動更新當前選中的小圓點

src/js/index.js

function Banner() {
        this.pageControl = $(".page-control");
};

Banner.prototype.animate = function () {
    var self = this;
    self.bannerUL.animate({"left":-798*self.index},500);
    // 通過index獲取到當前的li標簽,添加active樣式,兄弟li標簽移除active樣式
    self.pageControl.children('li').eq(self.index).addClass("active").siblings().removeClass("active");
};

Banner.prototype.listenPageControl = function () {
    var self = this;
    self.pageControl.children("li").each(function (index,obj) {
        $(obj).click(function () {
            self.index = index;
            self.animate();
        });
    });
};

Banner.prototype.run = function () {
    this.listenBannerHover();
};

src/css/scss.css

.header{
     z-index: 100;



.banner-group{

    overflow:hidden;
    z-index: 0;

4.11.實現自動循環無限輪播

src/js/index.js

function Banner() {
    this.index = 1;
};

Banner.prototype.initBanner = function () {
    var self = this;
    //實現無限輪播,原理:123-->>31231,在首尾克隆一張
    var firstBanner = self.liList.eq(0).clone();
    var lastBanner = self.liList.eq(self.bannerCount-1).clone();
    self.bannerUL.append(firstBanner);
    self.bannerUL.prepend(lastBanner);
    self.bannerUL.css({
        "width":self.bannerWidth*(self.bannerCount+2),
        "left":-self.bannerWidth,
    });
};

Banner.prototype.animate = function () {
    var self = this;
    self.bannerUL.animate({"left":-798*self.index},500);
    //小圓點的active
    var index = self.index;
    if(index === 0){
        index = self.bannerCount-1;
    }else if(index === self.bannerCount+1){
        index = 0;
    }else{
        index = self.index - 1;
    }
    // 通過index獲取到當前的li標簽,添加active樣式,兄弟li標簽移除active樣式
    self.pageControl.children('li').eq(index).addClass("active").siblings().removeClass("active");
};

Banner.prototype.loop = function(){
    var self = this;
    this.timer = setInterval(function () {
        if(self.index >= self.bannerCount+1){
            self.bannerUL.css({
                "left":-self.bannerWidth,
            });
            self.index = 2;
        }else{
            self.index++;
        }
        self.animate();
    },2000);
};

4.12.左右牽頭切換實現循環輪播

src/js/index.js

Banner.prototype.listenArrowClick = function () {
    var self = this;
    self.leftArrow.click(function () {
       if(self.index === 0){
           self.bannerUL.css({
              "left":-self.bannerCount*self.bannerWidth,
           });
           self.index = self.bannerCount - 1;
       }else{
           self.index --;
       }
       self.animate();
    });

    self.rightArrow.click(function () {
       if(self.index === self.bannerCount + 1){
           self.bannerUL.css({
              "left":-self.bannerWidth,
           });
           self.index = 2;
       }else{
           self.index ++;
       }
       self.animate();
    });
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM