美文网首页Web 前端开发 让前端飞
Vue如何写一个瀑布流加载

Vue如何写一个瀑布流加载

作者: BrightenSun | 来源:发表于2017-07-21 11:57 被阅读0次

一直用Vue做项目,需要做一个瀑布流加载,虽然以前也写过瀑布流,但是当你从服务器请求数据在渲染在页面的时候会有高度不准确的问题,所以带来了一个全新的瀑布流。

用到Vue2和axios 这两个文件
Vue2地址:https://cn.vuejs.org/v2/guide/installation.html
axios地址:https://www.npmjs.com/package/axios

至于样式,作为一个有追求的前端咱们自己调下就好了

html代码

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <script type="text/javascript" src="js/vue.min.js"></script>
        <script type="text/javascript" src="js/axios.min.js"></script>
        <link rel="stylesheet" type="text/css" href="css/global.css">
        <style>
            ul{
                width:calc(100% / 2);
            }
            ul img{
                width:100%;
            }
            ul p{
                text-align:center;
            }
        </style>
    </head>
    <body>
        <div id="app" v-cloak>
            <div class="clearFix">
                <ul class="left fl">

                </ul>
                <ul class="right fl">

                </ul>
                <ul class="list fl">
                    <li v-for="item in items">
                        <img :src=item.imgsrc alt="">
                        <div>
                            <p>{{item.msg}}</p>
                        </div>
                    </li>
                </ul>
            </div>
            <input type="button" name="" value="添加" @click="getList">
        </div>

        <script type="text/javascript">
            new Vue({
                el:"#app",
                created(){
                    this. getList();
                },
                data:{
                    items:[]
                },
                methods:{
                    getList(){
                        var _this = this;
                        axios.get('index.php')
                          .then(function (response) {
                            for(var i=0;i<response.data.length;i++){
                                _this.items.push(response.data[i]);
                            }
                            setTimeout(function(){
                                var aLi = document.querySelectorAll('.list li');
                                var oLeft = document.querySelector('.left');
                                var oRight = document.querySelector('.right');
                                var aImg = document.querySelectorAll('.list li img');
                                
                                for(var i=0;i<aLi.length;i++){
                                    if(oLeft.offsetHeight > oRight.offsetHeight){
                                        oRight.appendChild(aLi[i]);
                                    }else{
                                        oLeft.appendChild(aLi[i]);
                                    }
                                }
                            },50)
                          })
                          .catch(function (error) {
                            console.log(error);
                          });
                    }
                }
            })
        </script>
    </body>
</html>

当然你需要一个后台文件,不会写的也没关系,我也是找我后台帮我写的,分享大家。

php文件

<?php
        header('content-type:text/html;charset=utf-8');
        $arr = array();
        $msg = array(
            0=>"item1",
            1=>"item2",
            2=>"item3",
            3=>"item4",
            4=>"item5",
            5=>"item6",
            6=>"item7",
            7=>"item8",
            8=>"item9",
            9=>"item10",
        );


        for($i=0;$i<10;$i++){
            $arr[$i] = array(
                 'imgsrc'=>'imagse/'.($i+1).'.jpg',
                 'msg'=>$msg[$i],
            );
        }
        echo json_encode($arr);

至于图片你不能再管我要了吧?不过我可以给你我的图片名字方便你把你的图片修改下名字即可。
img图片名称例子:1.jpg。 简约大方 。

相关文章

  • vue—插件

    1.懒加载 vue-lazyload 2.滚动瀑布流插件 vue-infinite-scroll 3.一个简单易用...

  • Vue如何写一个瀑布流加载

    一直用Vue做项目,需要做一个瀑布流加载,虽然以前也写过瀑布流,但是当你从服务器请求数据在渲染在页面的时候会有高度...

  • Vue<瀑布流加载效果>

    效果图: ?身为程序员不仅要'造轮子'玩,更要学会找轮子玩。今天有点闲,就想着学学怎么实现瀑布流效果,找了找发现用...

  • vue-waterfall-easy 图片显示不出来

    用vue-waterfall-easy 插件写一个瀑布流,图片都能加载正常,但是页面上就是不显示图片,后来查证发现...

  • 9-5bug处理

    关于RecyleView瀑布流滑动加载数据,顶部出现空白。 关于RecyleView瀑布流显示加载图片乱跳 下拉刷...

  • 瀑布流、木桶布局

    瀑布流 瀑布流效果代码 木桶布局 木桶布局效果(加载有点慢)代码

  • 新闻瀑布流懒加载

    新闻瀑布流懒加载效果 代码

  • 预加载、延迟加载、瀑布流、拖拽

    1.预加载-图片: 2.延迟加载 3.瀑布流 瀑布流: 物体大小: 4.拖拽 磁性吸附例子: 虚线框拖拽

  • 瀑布流组件初探-Vue.js

    waterfall-瀑布流组件 基于Vue.js的瀑布流组件 GitHub地址: https://github.c...

  • 2016笔记——UICollectionView代理

    最近整的UICollectionView,想要一个瀑布流,然后不是死的,而是根据网络图片加载后显示的瀑布流,鉴于对...

网友评论

    本文标题:Vue如何写一个瀑布流加载

    本文链接:https://www.haomeiwen.com/subject/hpaskxtx.html