前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >webAPP页面切换之滑动-swiper2(切换页面默认显示顶部)

webAPP页面切换之滑动-swiper2(切换页面默认显示顶部)

作者头像
tianyawhl
发布2019-04-04 16:31:47
2.2K0
发布2019-04-04 16:31:47
举报
文章被收录于专栏:前端之攻略

最近做一个web页面,希望在手机上能滑动切换页面,第一次这种要求,在网上找到了一个插件swiper,swiper2可以在电脑和手机上使用,于是选择这个插件,在使用这个插件的过程也遇到了很多问题,如滚动条很长,体验不好,最后采用js来控制高度,在此页面中也采用了bootstrap-table插件来固定表头,采用js动态的设置高度,当页面高度小时,有滚动条,页面高时就完全展示,为了体验好,当滚动到第一个页面底部时,滑动切换第二个页面的顶部,需要scrollTop实现

页面结构

代码语言:javascript
复制
<body class="container" style="">
<div class="swiper-pages swiper-container" style="">
<div class="list-unstyled swiper-wrapper"  >
	<div class="swiper-slide" style="">
	    <div class="content-slide">
        页面1
	    </div>
	</div>
    <div class="swiper-slide" style="">
	    <div class="content-slide">
        页面2
	    </div>
	</div>
    <div class="swiper-slide" style="">
	    <div class="content-slide">
        页面3
	    </div>
	</div>
</div>
</div>
</body>

swiper主要用到的样式

代码语言:javascript
复制
<style type="text/css">
.swiper-container{position:relative;}
.swiper-slide{width:100%;}
.pagination {
  position: absolute;
  z-index: 20;
  top: 35px;
  width: 100%;
  text-align: center;
}
.swiper-pagination-switch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #ecf0f0;
  margin: 0 10px;
  opacity: 0.8;
  border: 1px solid #999;
  cursor: pointer;
}
.swiper-active-switch {
  background: #ee8e27;
}
</style>

swiper主要用到js

代码语言:javascript
复制
<script type="text/javascript">
var mySwiper = new Swiper('.swiper-container', {
    loop: true,
    pagination: '.pagination',
    paginationClickable: true,
    onSlideChangeStart: function(swiper) {
        //$( '.swiper-container' ).scrollTop(0);这样是直接到顶部,往往会出现闪屏,
        $('.swiper-container').animate({ scrollTop: 0 }, 10); //动画慢慢过渡到顶部
    }
});
$('.swiper-container').css({ "height": $(window).height(), "overflow-y": "auto" });
$('.swiper-wrapper').css({ "height": $(window).height() });
$('.swiper-slide').css({ "height": $(window).height() })
//页面中含有echart图表,需要再调用swiper插件后再init 和setoption图表,否则图表在页面切换时不显示
var myLineChart = echarts.init(document.getElementById('linechart1'), theme);
myLineChart.setOption(option2);
ObjectResize(myLineChart.resize);
</script>

bootstrap-table插件用到js,动态控制页面的高度

代码语言:javascript
复制
<script type="text/javascript">
$(document).ready(function() {
    $('#qiliangqifei').bootstrapTable({ height: $('.qiliangqifei-tab').outerHeight() + 10 });

    if (($(window).height() - 330) > ($('.fixed-table-container').outerHeight())) {
        $('.fixed-table-container').css({ "height": $('.fixed-table-container').outerHeight() });
    } else {
        //alert($( '.fixed-table-container').outerHeight());
        $('.fixed-table-container').css({ "height": $(window).height() - 330 });
    };

});
</script>

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/10/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档