最近,公司的PM提了一个需求 自动获取七天新上传的而且审核经过的商品作成固定的连接的一个活动页面。当时想了一想就用vue作了,感受效果还行,在这分享一下我是如何作的 但愿对你们有一点点帮助。前端
所谓的活动页 首先第一步确定是把页面切出来,这里就是2*n页面 我这里用的就是grid布局(也能够用flex)我主要讲三个点:vue
因为后段传过来的图片大小不同,我就对图片作了作了一下优化 。整个图片在填充盒子的同时保留其长宽比
代码:ios
.product-img img { object-fit: contain; width: auto; height: auto; max-width: 100%; max-height: 100%; margin: 0 auto; }
为了避免让图片以为突兀 咱们能够给图片的盒子设置一个伪元素web
.product-img::after { content: ''; position: absolute; top: 0; left: 0; -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); z-index: 1000; width: 100%; height: 100%; border-radius: .1rem; background: rgba(85, 85, 85, 0.05); }
因为从后台获取数据须要必定的时间 当数据没加载进来的时候会出现问题(也许就是一秒但这也会给用户带来很差的体验感。)axios
<div class="container" :class="productList.length ? 'show': ''">
当数据没加载的时候我就设置opacity为0,当数据出来的时候就设置opacity:1api
因为设计稿的需求是价格的整数的字体要比小数要大,因此就把整数和小数分别用spilt分隔来了。而后在给整数的字体比小数点的字体大一号就好了。布局
<div class="product-price">¥<span class="em">{{String(product.price).split('.')[0]}}</span>.{{String(product.price).split('.')[1]||'0'}}/天</div>
从后台获取数据是很重要的一部分 因为后段给了二个参数 一个是当前页 一个是一个页面有多少条数据。学习
getList(cb){ this.getActivityInfoById(this.curPage,this.pageSize).then((data = {})=>{ this.total = data.total; if(( this.curPage * this.pageSize) >= this.total && document.readyState == "complete") { this.isMaxPage = true; } this.productList = this.productList.concat(data.rows || []); cb && cb(data) }) }
getActivityInfoById: function(start, length) { return axios .get(this.api.getActivityInfoById, { params: { start: start, length: length } }) .then(function(res) { return res.data.data; }); },
所谓的活动页确定要作分页处理字体
onPage(){ const scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; const bodyHeight = document.body.offsetHeight; const clientHeight = window.innerHeight; if(scrollTop + clientHeight < bodyHeight){ return; } if(this.isGetList) return; if(this.total < this.curPage * this.pageSize){ return; } this.curPage++; this.isGetList = true; this.getList(()=>{ this.isGetList = false; }); },
当数据还在加载中显示loading,当数据加载完成是显示扯到底了flex
<div class="footer" v-if="isMaxPage">- 不要扯了 已经扯到底了 -</div> <div class="footer" v-if="!isMaxPage">- loading -</div>
因为这个活动页图片有点多 因此用了懒加载
lazyLoad: function() { var seeHeight = document.documentElement.clientHeight; // 可见区域高度 var imgs = document.getElementsByTagName('img'); for (var i = this.lazyLoadIndex; i < imgs.length; i++) { if ( imgs[i].getBoundingClientRect().top < seeHeight && imgs[i].dataset.src && imgs[i].getAttribute('src') !== imgs[i].dataset.src ) { imgs[i].setAttribute('src', imgs[i].dataset.src); this.lazyLoadIndex++; } } },
做为一个即将毕业的大四学生,这是我来公司实习作的活动页,但愿能够帮助你们,互相学习,一块儿进步。固然也有一些不足之处,请你们多多指教。码字不容易,但愿你们点个赞。前端路漫漫,与君共勉之。