layui下的图片轮播图效果代码收藏

以下展示在layui table表格列表中展示图片集,并使用layui图片轮播效果。

<script>
layui.use(['table', 'tree', 'layer','jquery'], function () {

//改变宽高
        var h = document.body.clientHeight&&document.documentElement.clientHeight - 80;//窗口高度
         var w = document.body.clientWidth&&document.documentElement.clientWidth - 10;//窗口高度
         //监听改变窗口大小
         window.onresize = function(){
            h = document.body.clientHeight&&document.documentElement.clientHeight - 80;//窗口高度
            w = document.body.clientWidth&&document.documentElement.clientWidth - 10;//窗口高度
            table.reload('idTest',{height:h,width: w});//刷新数据表格
         }

//方法级渲染
        window.demoTable = table.render({
            elem: '#idTest'
            ,id:'idTest'
            ,url : '<%=path%>/activity/getOrglifeListByOrgCode'
            ,width : w
            ,height : h
            , cols: [[ //标题栏
                 {checkbox: true, LAY_CHECKED: false, filter: 'test'}
               // , {field: 'ID', title: '序号', width: 150, sort: true, align: 'center'}
                , {field: 'PO_NAME', title: '组织名称', width: 250, align: 'center'}
                , {field: 'SUBJECT', title: '活动主题', width: 120, align: 'center'}
                , {field: 'PO_LIFE_TYPE_NAME', title: '生活类型', width: 180, align: 'center'}
                , {field: 'POLIFETIME', title: '活动时间', width: 180, align: 'center'}
                , {field: 'VENUE', title: '活动地点', width: 100, align: 'center'}
                , {field: 'PHOTO_UUID', title: '图片集', width: 200, align: 'center',templet:'#imgs'}
                , {field: 'ISOPEN', title: '是否开放', width: 120, align: 'center',templet:'#isopen'}
                , {field: 'PM_NAME', title: '主持人', width: 150, align: 'center'}
                , {field: 'partake', title: '参与人', width: 150, align: 'center',templet:'#join'}
                , {field: 'CREATETIME', title: '记录时间', width: 220, align: 'center'}
                , {field: 'PM_NUM', title: '参与数', width: 85, align: 'center'}
                , {title: '操作', width: 130, align: 'center', toolbar: '#barDemo'}
            ]]
            , page: true //是否显示分页
            ,limits : [ 10, 20,50, 100 ]
            ,limit : 20
            ,done:function(res,curr,count){
                $('table tr').on('click',function(){
                     $('table tr').css('background','');
                     $(this).css('background','<%=PropKit.use("config.properties").get("table_color")%>');
                 });
            }
        });

})

</script>

其中查看图片集使用的是Layui templet,id号为imgs,如下所示

<script type="text/html" id="imgs">
       <a class="openimg" href='javascript:;' onclick='showImages("{{d.PHOTO_UUID}}")' lay-event="img">查看图片集</a>
</script>

在列表显示中为超链接,点击事件调用了showImages方法。如下:

//图片弹窗
    function showImages(uuid){
        $.ajax({
            cache:false,
            type:'POST',
            dataType:"json",
            url:'<%=path%>/activity/showMediaFiles',
            data:{"uuid":uuid},
            error: function () {
                alert('请求失败');
            },
            success:function(res){
                console.log(res)
                layer.photos({
                    photos: res
                    ,anim: 5 //0-6的选择,指定弹出图片动画类型,默认随机(请注意,3.0之前的版本用shift参数)
                  }); 
            }
        });
    }

根据官方的说法,使用layer.photos插件,数据组织有一定的标准性,如下是官网的数据格式实例:

{
  "title": "", //相册标题
  "id": 123, //相册id
  "start": 0, //初始显示的图片序号,默认0
  "data": [   //相册包含的图片,数组格式
    {
      "alt": "图片名",
      "pid": 666, //图片id
      "src": "", //原图地址
      "thumb": "" //缩略图地址
    }
  ]
}

本人使用的是Jfinal框架,在<%=path%>/activity/showMediaFiles中组装好layui photo需要的json格式数据即可。

// 组织生活查看图片集
    public void showMediaFiles(){
        String orgCode = getSessionAttr("orgCode");
        String uuid = getPara("uuid");
        // 以下是自己封装的一个远程调用方法,具体底层还是Jfinal HttpKit.post(url,data)的调用
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("USER_ID", orgCode);
        map.put("UUID", uuid);
        String res_showMediaFiles = sendPostRequest("MgrActivityApi", orgCode, "showMediaFiles", "857", map);
        JSONObject jo_showMediaFiles = JSONObject.fromObject(res_showMediaFiles);

        JSONArray arr_photoes = new JSONArray();
        JSONObject obj_photo = new JSONObject();
        if(jo_showMediaFiles.getInt("code")==0){
            JSONArray photoes = jo_showMediaFiles.getJSONArray("data");
            for(int i =0 ;i<photoes.size();i++){
                JSONObject photo = photoes.getJSONObject(i);
                obj_photo.put("alt", "图片名");
                obj_photo.put("pid", 666);
                obj_photo.put("src", PropKit.use("config.properties").get("url")+getSessionAttr("REGION_CODE")+"/"+photo.getString("FILE_PATH"));
                obj_photo.put("thumb", "");
                arr_photoes.add(obj_photo);// 追加到JSONArray中
            }
        }
        Map<String,Object> dataMap = new HashMap<String,Object>();
        // 组装layer.photo需要的json格式数据
        dataMap.put("title", "");
        dataMap.put("id", 123);
        dataMap.put("start", 0);
        dataMap.put("data", arr_photoes);
        System.out.println(JsonKit.toJson(dataMap));
        renderJson(JsonKit.toJson(dataMap));
    }

最终显示效果如下:
这里写图片描述

点击其中的一列,弹出图片轮播的效果,多张图片可以左右滑动。

这里写图片描述

专业墙纸贴纸厨房用具装饰出售,本人网店经营

博客对你有用记得访问下哦,增加下访问量,如有需要可以下单购买哦^_^。https://item.taobao.com/item.htm?id=569617707364

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Layui框架提供了多种轮播图效果,其中常用的是基于carousel模块的轮播效果。下面详细介绍一下如何使用Layui实现轮播图效果。 1. 引入Layui库 在HTML文件中引入Layui库的CSS和JS文件,例如: ```html <link rel="stylesheet" href="layui/css/layui.css"> <script src="layui/layui.js"></script> ``` 2. 编写HTML结构 在HTML文件中编写轮播图的HTML结构,例如: ```html <div class="layui-carousel" id="test1"> <div carousel-item> <div>轮播1</div> <div>轮播2</div> <div>轮播3</div> <div>轮播4</div> <div>轮播5</div> </div> </div> ``` 其中,`layui-carousel`是轮播图的父容器,`test1`是轮播图的ID,`carousel-item`是轮播图的子容器,包含了多个轮播项。 3. 初始化轮播图 在JavaScript文件中,使用Layui的carousel模块初始化轮播图,例如: ```javascript layui.use('carousel', function(){ var carousel = layui.carousel; //建造实例 carousel.render({ elem: '#test1', width: '100%', height: '500px', interval: 5000, arrow: 'always', indicator: 'inside', autoplay: true }); }); ``` 其中,`elem`指定轮播图的ID,`width`和`height`分别指定轮播图的宽度和高度,`interval`指定轮播的间隔时间,`arrow`指定箭头显示的方式,`indicator`指定指示器显示的位置,`autoplay`指定是否自动播放。 4. 完整代码示例 ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Layui轮播图效果代码详解</title> <link rel="stylesheet" href="layui/css/layui.css"> </head> <body> <div class="layui-carousel" id="test1"> <div carousel-item> <div><img src="https://cdn.pixabay.com/photo/2016/01/05/17/51/apple-1122538_960_720.jpg" alt=""></div> <div><img src="https://cdn.pixabay.com/photo/2015/03/26/09/40/technology-690119_960_720.jpg" alt=""></div> <div><img src="https://cdn.pixabay.com/photo/2017/05/31/18/38/industry-236580_960_720.jpg" alt=""></div> <div><img src="https://cdn.pixabay.com/photo/2016/11/19/17/14/breakfast-1846110_960_720.jpg" alt=""></div> </div> </div> <script src="layui/layui.js"></script> <script> layui.use('carousel', function(){ var carousel = layui.carousel; //建造实例 carousel.render({ elem: '#test1', width: '100%', height: '500px', interval: 5000, arrow: 'always', indicator: 'inside', autoplay: true }); }); </script> </body> </html> ``` 以上就是使用Layui框架实现轮播图效果的详细代码解析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值