layui 轮播图写法及图片不显示问题解决

<!--轮播图-->
        <div class="aui-banner-content" data-aui-slider style="height: 24%;margin-top: 0">
            <div class="layui-carousel" id="zqzj" style="border-radius: 2vw;width: 100%">
                <div carousel-item id="swiper-wrapper">

                </div>
            </div>
        </div>

 

//轮播图
var ins = "";
var scrollTop = 0;//轮播图上下滑动
function selectHomeSlideshow() {
    $.post("url", {
        "type": 0,
    }, function (data) {
        var table = '';
        $.each(data, function (i, list) {
            table += "<img src=\"" + url + list.indexImg + "\" style=\"width: 100%; border-radius:10px;\">\n"
        })
        $("#swiper-wrapper").html(table);

        //序列话一次轮播图
        layui.use(['carousel'], function () {
            var carousel = layui.carousel;
            ins = carousel.render({
                elem: '#zqzj'
                , width: '100%' //设置容器宽度
                , height: '49vw'
                , anim: 'default'
                , indicator: 'inside'
                , arrow: 'none'
                , interval: 3000
            });
        })
        // 监听滑动事件切换轮播图
        $("#zqzj").on("touchstart", function (e) {
            var startX = e.originalEvent.targetTouches[0].pageX;//开始坐标X
            var touch = e.touches[0];
            var startY = touch.pageY;
            $(this).on('touchmove', function (e) {
                arguments[0].preventDefault();//阻止手机浏览器默认事件

                var touch = e.touches[0];
                var deltaY = touch.pageY - startY;

                scrollTop += -deltaY
                document.documentElement.scrollTop = scrollTop
                document.body.scrollTop = scrollTop
            });
            $(this).on('touchend', function (e) {
                var endX = e.originalEvent.changedTouches[0].pageX;//结束坐标X
                e.stopPropagation();//停止DOM事件逐层往上传播
                if (endX - startX > 30) {
                    ins.slide("sub");
                } else if (startX - endX > 30) {
                    ins.slide("add");
                }
                $(this).off('touchmove touchend');
            });
        })

        // 重置轮播,不然有可能不显示
        ins.reload({
            elem: '#zqzj'
            , width: '100%' //设置容器宽度
            , height: '49vw'
            , anim: 'default'
            , indicator: 'inside'
            , arrow: 'none'
            , interval: 3000
        });
    })
}

图片不显示就在方法最后重置一下。记得引入layui 库

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值