vue2.0高仿饿了么better-scroll
首先安装better-scroll
npm i better-scroll -S
goods页面模板
<template>
<div class="goods">
<div class="menu-wrapper" ref="menuWrapper">
<ul>
<li v-for="item in goods" class="menu-item">
<span class="text border-1px">
<span v-show="item.type>0" class="icon" :class="classMap[item.type]"></span>{{item.name}} </span> </li>
</ul>
</div>
<div class="foods-wrapper" ref="foodsWrapper">
<ul>
<li v-for="item in goods" >
<h1 class="title">{{item.name}}</h1>
<ul>
<li v-for="food in item.foods" class="food-item border-1px">
<div class="icon">
<img :src="food.icon" alt="" width="57" height="57">
</div>
<div class="content">
<h2 class="name">{{food.name}}</h2>
<p class="desc">{{food.description}}</p>
<div class="extra">
<span class="food-number">月售{{food.sellCount}}份</span>
<span>好评率{{food.rating}}%</span>
</div>
<div class="price">
<span class="nowPrice">¥{{food.price}}</span>
<span class="oldPrice">¥{{food.oldPrice}}</span>
</div>
</div> </li>
</ul>
</li>
</ul> </div>
</div>
</template>
js
<script type="text/ecmascript-6">
/* eslint-disable*/
import axios from 'axios'
import BScroll from 'better-scroll'
export default{ props:{
seller:{
type:Object
}
},
data(){
return{
goods:[]
}
},
created(){
this.classMap=['decrease', 'discount', 'special', 'invoice', 'guarantee']
axios.get('/api/goods').then((res)=>{
this.goods=res.data.data;
this.$nextTick(()=>{
this._initScroll();
})
console.log(this.$refs.menuWrapper) }) },
methods:{
_initScroll(){
this.meunScroll=new BScroll(this.$refs.menuWrapper,{});
this.foodsScroll=new BScroll(this.$refs.foodsWrapper,{});
} }
}
</script>
better-scroll用法
我们先来看一下 better-scroll 常见的 html 结构:
<div class="wrapper">
<ul class="content">
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
当 content 的高度不超过父容器的高度,是不能滚动的,而它一旦超过了父容器的高度,我们就可以滚动内容区了,这就是 better-scroll 的滚动原理。
import BScroll from 'better-scroll'
let wrapper = document.querySelector('.wrapper')
let scroll = new BScroll(wrapper, {})
better-scroll 对外暴露了一个 BScroll 的类,我们初始化只需要 new 一个类的实例即可。第一个参数就是我们 wrapper 的 DOM 对象,第二个是一些配置参数。
better-scroll 的初始化时机很重要,因为它在初始化的时候,会计算父元素和子元素的高度和宽度,来决定是否可以纵向和横向滚动。因此,我们在初始化它的时候,必须确保父元素和子元素的内容已经正确渲染了。如果没有办法滑动,那就是初始化的时机不对。
饿了么是这样处理的:
mounted() {
this.$nextTick(() => {
this.scroll = new Bscroll(this.$refs.wrapper, {}) })
}
this.$nextTick()
这个方法作用是当数据被修改后使用这个方法会回调获取更新后的dom再render出来
如果不在下面的this.$nextTick()
方法里回调这个方法,数据改变后再来计算滚动轴就会出错
vue2.0高仿饿了么better-scroll的更多相关文章
- 【饿了么】—— Vue2.0高仿饿了么核心模块&移动端Web App项目爬坑(三)
前言:接着上一篇项目总结,这一篇是学习过程记录的最后一篇,这里会梳理:评论组件.商家组件.优化.打包.相关资料链接.项目github地址:https://github.com/66Web/ljq_el ...
- 用vue2 +vue-router2 + es6 +webpack 高仿饿了么app(干货满满)
#高仿饿了么app商家详情 (vue2 +vue-router2 + es6 +webpack ) ##demo [demo 地址](http://liangxiaojuan.github.io/ ...
- 基于vue2+nuxt构建的高仿饿了么(2018版)
前言 高仿饿了么,以nuxt作为vue的服务端渲染,适合刚接触或者准备上vue ssr的同学参考和学习 项目地址如遇网络不佳,请移步国内镜像加速节点 效果演示 查看demo请戳这里(请用chrome手 ...
- Vue.js高仿饿了么WebApp
介绍 学习Vue.js也有一阵子了,为了加深对Vue的理解及运用,做了一个小项目.这是一个高仿饿了么外卖WebApp,现已完成商品预览.商品详情.商家预览.添加购物.查看评论等功能. 部分截图 项目预 ...
- Vuejs 高仿饿了么外卖APP 百度云视频教程下载
Vuejs 高仿饿了么外卖APP 百度云视频教程下载 链接:https://pan.baidu.com/s/1KPbKog0qJqXI-2ztQ19o7w 提取码: 关注公众号[GitHubCN]回复 ...
- 使用 swift3.0高仿新浪微博
项目地址:https://github.com/SummerHH/swift3.0WeBo 使用 swift3.0 高仿微博,目前以实现的功能有,添加访客视图,用户信息授权,首页数据展示(支持正文中连 ...
- vue2高仿饿了么app
Github地址: https://github.com/ccyinghua/appEleme-project 一.构建项目所用: vue init webpack appEleme-project ...
- 基于vue2.0实现仿百度前端分页效果(二)
前言 上篇文章中,已经使用vue实现前端分页效果,这篇文章我们单独将分页抽离出来实现一个分页组件 先看实现效果图 代码实现 按照惯例,我们在冻手实现的时候还是先想一想vue实现组件的思路 1.需要提前 ...
- 基于vue2.0实现仿百度前端分页效果(一)
前言 最近在接手一个后台管理项目的时候,由于之前是使用jquery+bootstrap做的,后端使用php yii框架,前后端耦合在一起,所以接手过来之后通过vue进行改造,但依然继续使用的boots ...
随机推荐
- StampedLock原理
原文链接:https://blog.csdn.net/sunhaoning/article/details/68924625 StamppedLock是Java 8中引入的一种新的锁机制.读写锁虽然分 ...
- POI生成Excel强制换行
自动换行的设置: HSSFCellStyle cellStyle=workbook.createCellStyle(); cellStyle.setWrapText(true); cell.setCe ...
- Redis 工具类 java 实现的redis 工具类
最近了解了一下非关系型数据库 redis 会使用简单的命令 在自己本地电脑 使用时必须先启动服务器端 在启动客户端 redis 简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...
- mysql 常用函数。。
FIND_IN_SET(str,strlist) ,strlist 是 一个 由 逗号 分割的字符串,要注意 strlist 不能有逗号.. 它 等于 where str in (1,2,3***) ...
- Diophantus of Alexandria
Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...
- tomcat故障——数据库未授权
- unittest框架
在我们真正的编写测试用例之前,我们需要了解一下测试框架. unittest是python自带的单元测试框架,尽管其主要是为单元测试服务的,但我们也可以用它来做接口的自动化测试. unittest框架为 ...
- RobotFramework AppiumLibrary 用户关键字
RobotFramework AppiumLibrary 用户关键字 最近在用robotframework搞安卓app 的自动化,其中用到了 AppiumLlibrary 的关键字,我都映射成了用户关 ...
- Readthedocs+Github搭建文档
一.文档撰写前提 环境部署: > git clone https://github.com/toooney/demo-readthedocs.git > pip install sphin ...
- Mybatis学习笔记2 - 解析config
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC ...