swiper动态加载数据修改数据,loop模式循环

需求:加载页面,请求所有的列表数据竖直显示,超过5个,开始轮播;点击按钮,筛选数据,不足5个的话不轮播,如果超过则开始滚动;swiper 4.0*

swiper轮播

HTML

 <div class="project_list_wrap project_list_wrap2 swiper-container">
          <div class="swiper-wrapper">
          </div>
    </div>

CSS

.project_list_wrap2 {
  height: 350px;
}

.project_list_wrap2.no_swiper {
  height: auto;
}

.no_swiper .swiper-wrapper {
  flex-direction: column;
}

.no_swiper .swiper-wrapper .project_item {
  height: 60px;
  margin-bottom: 10px;
}

JS

let listData1 = [{
  title_1: '江汉区老年人能力评估点位审批111',
  titel_2: '武汉市老年人能力评估',
  source: '2019年武汉市养老项目工作',
  date: '昨天 09:00 - 周三'
}, {
  title_1: '江汉区老年人能力评估点位审批222',
  titel_2: '武汉市老年人能力评估',
  source: '2019年武汉市养老项目工作',
  date: '昨天 09:00 - 周三'
}, {
  title_1: '江汉区老年人能力评估点位审批333',
  titel_2: '武汉市老年人能力评估',
  source: '2019年武汉市养老项目工作',
  date: '昨天 09:00 - 周三'
}, {
  title_1: '江汉区老年人能力评估点位审批444',
  titel_2: '武汉市老年人能力评估',
  source: '2019年武汉市养老项目工作',
  date: '昨天 09:00 - 周三'
}, {
  title_1: '江汉区老年人能力评估点位审批555',
  titel_2: '武汉市老年人能力评估',
  source: '2019年武汉市养老项目工作',
  date: '昨天 09:00 - 周三'
}, {
  title_1: '江汉区老年人能力评估点位审批666',
  titel_2: '武汉市老年人能力评估',
  source: '2019年武汉市养老项目工作',
  date: '昨天 09:00 - 周三'
}];
let listData2 = [{
    title_1: '江汉区老年人能力评估点位审批aaaa',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  }, {
    title_1: '江汉区老年人能力评估点位审批bbbb',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  }, {
    title_1: '江汉区老年人能力评估点位审批cccc',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  }
  /* , {
    title_1: '江汉区老年人能力评估点位审批dddd',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  }, {
    title_1: '江汉区老年人能力评估点位审批eeee',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  }, {
    title_1: '江汉区老年人能力评估点位审批ffff',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  }, {
    title_1: '江汉区老年人能力评估点位审批gggg',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  }, {
    title_1: '江汉区老年人能力评估点位审批hhhh',
    titel_2: '武汉市老年人能力评估',
    source: '2019年武汉市养老项目工作',
    date: '昨天 09:00 - 周三'
  } */
];
//app全局属性
const app = {};
app.html = {};
app.html.str = '';
app.toggle = true;
app.swiper = null;
app.swiperViewLimitNum = 5; //轮播触发的个数
app.swiperContainerClass = '.project_list_wrap2 '
app.swiperWrapperClass = '.project_list_wrap2 .swiper-wrapper';
//初始化页面
function initPage() {
//加载数据
  beforeTodayListGetData(listData1);
//点击按钮,切换数据
  $('.toggleData').on('click', function () {
    // debugger;
    if (app.toggle) {
      beforeTodayListGetData(listData2);
      app.toggle = false;
    } else {
      beforeTodayListGetData(listData1);
      app.toggle = true;
    }
  })
}
//获取数据渲染页面
function beforeTodayListGetData(data) {
  const listData = data;
  let dataLen = listData.length; //数据的个数
  let dataListWrap = $(app.swiperWrapperClass);
  let containerWrap = $(app.swiperContainerClass);

  dataListWrap.empty(); //清空页面的slides
  app.html.str = '';

  for (let i = 0; i < listData.length; i++) {
    app.html.str += '<div class="swiper-slide project_item">' +
      '<div class="project_item_title_1 noWrap">' + listData[i].title_1 + '</div>' +
      '<div class="project_item_title_2 noWrap">' + listData[i].titel_2 + '</div>' +
      '<div class="project_item_source">' + listData[i].source + '</div>' +
      '<div class="project_item_date">' + listData[i].date + '</div>' + '</div>';
  };
  //如果数据条数少于swiperViewLimitNum
  if (dataLen <= app.swiperViewLimitNum) {
    if (app.swiper) {
      app.swiper.destroy(true, true); //销毁swiper
      app.swiper = null;
    }
    /* 不是swiper的情况 */
    dataListWrap.append(app.html.str);
    containerWrap.addClass('no_swiper');
    $(app.swiperContainerClass).off('mouseover').off('mouseleave'); //解除事件绑定
  } else {
    containerWrap.removeClass('no_swiper'); //清除class类
    setTimeout(() => {//渲染页面需要时间
      if (app.swiper) {
        //数据满足swiperViewLimitNum,可以轮播
        app.swiper.update();
        app.swiper.appendSlide(app.html.str); //添加slide
      } else {
        dataListWrap.append(app.html.str);
        initSwiper()
      }
    }, 100);
  }
}


//初始化轮播
function initSwiper() {
  //配置项
  let options = {
    direction: "vertical",
    initialSlide: 0, //自动滚动
    autoplay: {
      delay: 2000
    },
    loop: true, //形成环路
    observer: true,
    slidesPerView: app.swiperViewLimitNum,//可以显示的slides个数
    spaceBetween: 10,//slides间隔
  }
  //初始化swiper
  app.swiper = new Swiper(app.swiperContainerClass, options);
  
  //绑定事件
  //鼠标移入暂停轮播
  $(app.swiperContainerClass).on('mouseover', function () { 
    app.swiper.autoplay.stop();
  })
   //鼠标移出开始轮播
  $(app.swiperContainerClass).on('mouseleave', function () {
    app.swiper.autoplay.start();
  })
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值