查看: 22543|回复: 11
打印 上一主题 下一主题

swiper-container搞定自适应

[复制链接]

该用户从未签到

1

主题

5

帖子

21

积分

新手上路

Rank: 1

积分
21
跳转到指定楼层
楼主
发表于 2015-9-13 14:47:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5金钱
我是想弄成swiperde 高度根据silder的高度自适应。

也看了这里的教程:http://www.swiper.com.cn/api/method/2015/0308/241.html
用了源码
  1.             onInit: function(swiper){
  2.                 swiper.container[0].style.height=swiper.slides[swiper.activeIndex].offsetHeight+'px';
  3.             },
  4.             onSlideChangeEnd: function(swiper){
  5.                 swiper.container[0].style.height=swiper.slides[swiper.activeIndex].offsetHeight+'px';
  6.             },
复制代码
但是切换后高度不变啊。求教!谢谢

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖
回复

使用道具 举报

该用户从未签到

2

主题

13

帖子

190

积分

注册会员

Rank: 2

积分
190
沙发
发表于 2015-9-15 11:18:17 | 只看该作者
.swiper-container  {height:100%}
回复

使用道具 举报

  • TA的每日心情
    慵懒
    2019-8-26 15:39
  • 签到天数: 1 天

    [LV.1]初来乍到

    0

    主题

    12

    帖子

    96

    积分

    注册会员

    Rank: 2

    积分
    96
    板凳
    发表于 2015-9-19 15:46:36 | 只看该作者
    onSlideChangeEnd  不能触发....  怎么没人回答呢?帮帮我们这些菜鸟啊
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    2019-8-26 15:39
  • 签到天数: 1 天

    [LV.1]初来乍到

    0

    主题

    12

    帖子

    96

    积分

    注册会员

    Rank: 2

    积分
    96
    地板
    发表于 2015-9-19 17:03:19 | 只看该作者
    疯子蚁 发表于 2015-9-19 15:46
    onSlideChangeEnd  不能触发....  怎么没人回答呢?帮帮我们这些菜鸟啊

    http://www.swiper.com.cn/api/touch/2015/0308/205.html  看我直播  现在算出的高度竟然一样的 继续
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    2019-8-26 15:39
  • 签到天数: 1 天

    [LV.1]初来乍到

    0

    主题

    12

    帖子

    96

    积分

    注册会员

    Rank: 2

    积分
    96
    5#
    发表于 2015-9-21 10:39:45 | 只看该作者
    本帖最后由 疯子蚁 于 2015-9-21 10:42 编辑
    1. var eHeight0,eHeight1,eHeight2;
    2.                         var mySwiper = new Swiper('.swiper-container', {
    3.                                 resistanceRatio : 1,
    4.                                 onInit: function(swiper){
    5.                                         eHeight0 = swiper.slides[0].offsetHeight+'px';
    6.                                         eHeight1 = swiper.slides[1].offsetHeight+'px';
    7.                                         eHeight2 = swiper.slides[2].offsetHeight+'px';
    8.                                         alert(eHeight2)
    9.                                         switch(swiper.activeIndex)
    10.                                         {
    11.                                                 case 0:
    12.                                                 swiper.container[0].style.height=eHeight0;
    13.                                                 break;
    14.                                                 case 1:
    15.                                                 swiper.container[0].style.height=eHeight1;
    16.                                                 break;
    17.                                                 case 2:
    18.                                                 swiper.container[0].style.height=eHeight2;
    19.                                                 break;
    20.                                         }
    21. //                swiper.container[0].style.height=swiper.slides[swiper.activeIndex].offsetHeight+'px';
    22.             },onSlideChangeStart: function(swiper){
    23.                             //var eHeight0 = swiper.slides[0].offsetHeight+'px';
    24.                                         //var eHeight1 = swiper.slides[1].offsetHeight+'px';
    25.                                         //var eHeight2 = swiper.slides[2].offsetHeight+'px';
    26.                                         switch(swiper.activeIndex)
    27.                                         {
    28.                                                 case 0:
    29.                                                 swiper.container[0].style.height=eHeight0;
    30.                                                 alert('0');
    31.                                                 break;
    32.                                                 case 1:
    33.                                                 swiper.container[0].style.height=eHeight1;
    34.                                                 alert('1')
    35.                                                 break;
    36.                                                 case 2:
    37.                                                 swiper.container[0].style.height=eHeight2;
    38.                                                 alert('2')
    39.                                                 break;
    40.                                         }
    41. //                swiper.container[0].style.height=swiper.slides[swiper.activeIndex].offsetHeight+'px';
    42. //                alert(swiper.activeIndex);
    43. //              alert(swiper.slides[swiper.activeIndex].offsetHeight+'px');
    44.                 //alert(swiper.slides[swiper.activeIndex].offsetHeight+'px');
    45.             },
    46.                                 direction: 'horizontal',
    47.                                 loop: false,
    48.                                 calculateHeight: true,
    49.                                 effect: 'coverflow',
    50.                                 useCSS3Transforms:'false',
    51.                                 // 如果需要分页器
    52.                                 pagination: '.swiper-pagination',
    53.                                 // 如果需要前进后退按钮
    54.                                 // nextButton: '.swiper-button-next',
    55.                                 // prevButton: '.swiper-button-prev',
    56.                                 // 如果需要滚动条
    57.                                 //                                 scrollbar: '.swiper-scrollbar',
    58.                         })
    复制代码
    先将就吧  等大神来
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    2019-8-26 15:39
  • 签到天数: 1 天

    [LV.1]初来乍到

    0

    主题

    12

    帖子

    96

    积分

    注册会员

    Rank: 2

    积分
    96
    6#
    发表于 2015-9-24 10:35:34 | 只看该作者
    疯子蚁 发表于 2015-9-21 10:39
    先将就吧  等大神来

    我又来了   这么写火狐都不认识.
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-10-14 22:14
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    83

    主题

    391

    帖子

    4万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    44024
    7#
    发表于 2015-9-28 17:09:26 | 只看该作者
    <div class="swiper-wrapper" style="float:left; height:auto;">
    回复

    使用道具 举报

    该用户从未签到

    0

    主题

    1

    帖子

    11

    积分

    新手上路

    Rank: 1

    积分
    11
    8#
    发表于 2015-12-11 13:18:22 | 只看该作者
    本帖最后由 阳光不锈 于 2015-12-11 13:20 编辑

    经验证,没必要给swiper-wrapper额外添加样式,只需要在onInit的函数中设置每个swiper-slide的高度,然后在设置API接口中autoHeight:true即可.
    onInit: function(swiper){
                        var arr=[];
                        $(".swiper-slide").map(function(i){
                            var len=$(this).find(".grid-packery").length;
                            arr.push(len);
                            $(this).css("height",gridHeight*parseInt(arr)+'px');
                        });
                    },
    因为我的每个slide中放的是n个大小一样的div所以就按gridHeight*parseInt(arr)这样计算了
    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2017-2-10 09:17
  • 签到天数: 122 天

    [LV.7]常住居民III

    4

    主题

    150

    帖子

    3243

    积分

    论坛元老

    Rank: 8Rank: 8

    积分
    3243
    9#
    发表于 2015-12-11 16:53:07 | 只看该作者
    看看不知道会不会给分~
    回复

    使用道具 举报

    该用户从未签到

    0

    主题

    2

    帖子

    14

    积分

    新手上路

    Rank: 1

    积分
    14
    10#
    发表于 2016-10-15 16:22:14 | 只看该作者
    加一个这个就可以了。autoHeight: true,

    点评

    评分: 5.0
    评分: 5
    这才是我要的解决办法,简单明了!!  发表于 2018-5-28 14:08
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|Swiper中文网 ( 粤ICP备15001020号

    GMT+8, 2024-5-3 22:29 , Processed in 0.076202 second(s), 26 queries .

    Powered by Discuz! X3.2

    © 2001-2013 Comsenz Inc.

    快速回复 返回顶部 返回列表