This article mainly introduces the detailed explanation of the goodvs component development of the Vue framework. Now I will share it with you and give you a reference.
1. Layout Flex
Flex layout can realize various page layouts simply, completely and responsively. Flex is the abbreviation of Flexible Box, which means "elasticity" Layout" to provide maximum flexibility for box models. Any container can be designated as a Flex layout.
1 2 |
|
1 2 |
|
// Main attributes
flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis' > ]
The flex attribute is the abbreviation of flex-grow, flex-shrink and flex-basis. The default value is 0 1 auto. The last two properties are optional.
The flex-grow attribute defines the enlargement ratio of the item. The default is 0, that is, if there is insufficient space, the item will not be enlarged.
The flex-shrink attribute defines the shrinkage ratio of the item. The default is 1, that is, if there is insufficient space, the item will not be enlarged. Will shrink, the flex-shrink attribute is 0, and other items are all 1. If there is insufficient space, the former will not shrink
The flex-basis attribute defines the main axis space (main size) occupied by the item before allocating excess space. The browser uses this attribute to calculate whether there is extra space on the main axis. Its default value is auto, which is the original size of the project. If it is set to the same value as the width or height attribute (such as 350px), the project will occupy a fixed space
2. Icon component
Sub component iconMap
1 2 3 |
|
1 2 3 4 5 6 7 8 |
|
Parent component goods
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 |
|
3. better-scroll application
Similar to iscroll implementation Scroll effect
Installation
1 |
|
Introduction
1 |
|
Description
(1) Principle: The parent container wrapper, which has a fixed height, when its If the height of a child element content exceeds the height of the wrapper, we can scroll the content area. If it does not exceed the height, we cannot scroll.
(2) Initialization of better-scroll
The initialization timing of better-scroll is very important, because when it is initialized, it will calculate the height and width of the parent element and child element to determine whether Can scroll vertically and horizontally. Therefore, when we initialize it, we must ensure that the content of the parent element and child element has been rendered correctly. If the DOM structure of the child element or parent element changes, the scroll.refresh() method must be called again and recalculated to ensure the normal scrolling effect. Therefore, the reason why better-scroll cannot scroll is probably because the timing of initializing better-scroll is wrong, or better-scroll is not recalculated when the DOM structure sends changes.
(3) better-scroll combined with Vue
Vue.js provides us with an interface to obtain DOM objects - vm.$refs. Here, we access the DOM object through this.$refs.wrapper, and we initialize better-scroll in the mounted hook function and the callback function of this.$nextTick. Because at this time, the wrapper's DOM has been rendered, we can correctly calculate the height of it and its inner content to ensure normal scrolling.
This.$nextTick here is an asynchronous function. In order to ensure that the DOM has been rendered, MutationObserver or setTimeout(fn, 0) is used at the bottom. In fact, we can replace this.$nextTick with setTimeout(fn, 20) here (20 ms is an experience value, each Tick is about 17 ms), which is imperceptible to the user experience.
(4) Processing of asynchronous data
In our actual work, the data in the list is often obtained asynchronously, so the timing for us to initialize better-scroll needs to be after the data is obtained. The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
The requestData here is pseudo code. Its function is to initiate an http request to obtain data from the server, and this function returns a promise (in actual projects, we may use axios or vue-resource ). After we obtain the data, we need to initialize better-scroll in an asynchronous manner, because Vue is data-driven. When Vue data changes (this.data = res.data) and the page is re-rendered, it is an asynchronous process. We The initialization time is after the DOM is re-rendered, so this.$nextTick is used here. Of course, it can be replaced with setTimeout(fn, 20).
Note: Why is the data requested in the created hook function instead of in the mounted hook function? Because requestData is sending a network request, which is an asynchronous process. When the response data is obtained, Vue's DOM has already been rendered, but the data changes -> DOM re-rendering is still an asynchronous process, so even if After we get the data, we also need to initialize better-scroll asynchronously.
Use
to initialize the dom structure that needs to be scrolled
Use the ref attribute to bind a certain dom element, or to bind a certain component, and then use this.$refs.menuwrapper to get the dom inside the function.
Note: If used on a normal DOM element, the reference points to the DOM element; if used on a sub-component, the reference points to the component instance:
1 2 |
|
Execute _initScroll within ajax () Function
Before this we need to make some preparations and precautions
(1) dom结构完全加载完再调用_initScroll()方法才会生效
(2) 因为要监听内容区域的高度,所以初始化应在created过程中去监听dom结构是否完全加载,这里是在$nextTick对象中进行触发检测
ES6语法格式: this.$nextTick(() => {})
1 2 3 4 5 6 7 8 9 |
|
(3) 在methods方法里面定义一个_initScroll的函数,主要用来对左右两侧dom结构进行初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
说明:vue中更改数据,DOM会跟着做映射,但vue更新DOM是异步的,用 $nextTick ()来确保Dom变化后能调用到_initScroll()方法。调用_initScroll()方法能计算内层ul的高度,当内层ul的高度大于外层wrapper的高度时,可以实现滚动。
此时俩侧可以分别滚动了!
(4) 实现左右联动
原理:我们计算出右侧实时变化的y值,落到哪一个区间,我们就显示那一个区间。首先我们要计算整体区间的一个高度,然后分别计算第一个区间的高度,第二个区间的高度,以此类推。然后将区间数存入一个定义好的数组。当我们在滚动的时候实时拿到y轴的高度,然后对比在哪一个区间,这样我们就会得到一个区间的索引值去对应左侧的菜品类别,最后我们用一个vue的class去绑定高亮文本。
1.定义一个方法在 _initScroll 下面,作为计算高度的方法叫做_calculateHeight () ,再定义一个listHeight:[]数组,存放获取到的每一块foods类的高度。然后通过给每个li 定义类名来供js 选择 从而计算出高度存放到listHeight数组里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
2.我们获取到区间高度数组后,我们要实时获取到右侧的y值,和左侧的索引值做一个对比,定义一个scrollY变量用来存放实时获取的y值。bs插件为我们提供了一个实时获取y值的方法,我们在初始化this.foodScroll的时候加一个·属性probeType: 3,其作用就是实时获取y值,相当于探针的作用。
1 2 3 |
|
1 2 3 4 5 |
|
3.我们再添加一个方法this.foodScroll.on('scroll',(pos) => {}),作用是实时滚动的时候把获取到的位置给暴露出来。代码如下。
1 2 3 4 5 6 |
|
4.定义一个计算属性computed,获取到food滚动区域对应的menu区域的子块的索引i值,从而定位到左侧边栏的位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
获取到i后,,然后通过设置一个class来做样式切换变化 :class="{'current':currentIndex === index}" ,当currentIndex和menu-item对应的index相等时,设置current的样式。这样就可以实现左右联动了。
1 2 |
|
在样式里提前设好 选中和正常的样式
5.最后实现左侧点击的功能。在左侧的li下绑定一个selectMenu的点击事件,并传入索引值,这样我们就可以知道点击的是哪一个li
1 2 |
|
1 2 3 |
|
1 2 3 |
|
参考: vue使用 better-scroll的参数和方法
6.关于在selectMenu中点击事件
在selectMenu中点击,在pc界面会出现两次事件,在移动端就只出现一次事件的问题
原因 : better-scroll 会监听事件(例如touchmove,click之类),并且阻止默认事件(prevent stop),并且他只会监听移动端的,pc端的没有监听
在pc页面上 better-scroll 也派发了一次click事件,原生也派发了一次click事件
1 2 3 4 |
|
解决 : 针对better-scroll 的事件,有_constructed: true,所以做处理,return掉非better-scroll 的事件
1 2 3 4 5 6 7 8 |
|
goods 组件到此差不多了!
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
The above is the detailed content of Development of goods components in Vue framework. For more information, please follow other related articles on the PHP Chinese website!