当前位置:网站首页>Slide the menu of uni app custom components left and right and click switch to select and display in the middle
Slide the menu of uni app custom components left and right and click switch to select and display in the middle
2022-06-26 03:43:00 【yyxhzdm】
design sketch :


One 、
1. Ideas : Calculate the width of each child element , Get the index of the currently clicked element when clicking , The distance between the currently clicked child element and the left column - scroll-view Half the width + Half the width of the currently clicked child element Center display .
Some simple data explanation :

2. Custom components HuaDongScrollviewCenter File code :
<template name="HuaDongScrollviewCenter">
<view class="center-cut-menu">
<scroll-view scroll-x="true" scroll-with-animation="true" class="scroll-view" :scroll-left="scrollLeft">
<view class="scroll-item" v-for="(item, index) in list" :key="index" @click="changeMenu(index)">
<text class="item-text" :class="curIndex == index? 'active' : ''">{ {item.name}}</text>
</view>
</scroll-view>
</view>
</template>
<script>
export default {
name: "HuaDongScrollviewCenter",
data() {
return {
contentScrollW: 0, // Navigation area width
curIndex: 0, // The current selection
scrollLeft: 0, // Horizontal scroll bar position
};
},
mounted() {
// Get the width of the title area , And the width of each child element node
this.getScrollW()
},
props: {
list: {} // Data set
},
methods: {
// Get the width of the title area , And the width of each child element node and the distance from the element to the left column
getScrollW() {
let query = uni.createSelectorQuery().in(this);
query.select('.scroll-view').boundingClientRect(data => {
// Get scroll-view Component width
this.contentScrollW = data.width
}).exec();
query.selectAll('.scroll-item').boundingClientRect(data => {
let dataLen = data.length;
for (let i = 0; i < dataLen; i++) {
// scroll-view The distance from the sub element component to the left column
this.list[i].left = data[i].left;
// scroll-view Sub element component width
this.list[i].width = data[i].width
}
}).exec()
},
// Choose the title
changeMenu(index) {
var _this = this
_this.curIndex = index;
// Effect 1 ( Currently, click the sub element to display on the left ) limitations : The child elements have the same width
// _this.scrollLeft = index * _this.list[index].width
// Effect 1 ( Currently, click the sub element to display on the left ) Different width of child elements can also be realized
// _this.scrollLeft = 0;
// for (let i = 0; i < index; i++) {
// _this.scrollLeft += _this.list[i].width
// };
// Effect two ( Currently click on the child element to leave a display on the left ) limitations : The child elements have the same width
// _this.scrollLeft = (index - 1) * _this.list[index].width
// Effect two ( Currently click on the child element to leave a display on the left ) Different width of child elements can also be realized
// _this.scrollLeft = 0;
// for (let i = 0; i < index - 1; i++) {
// _this.scrollLeft += _this.list[i].width
// };
// Effect three ( The currently clicked sub elements are displayed in the middle ) It is not affected by the width of child elements
_this.scrollLeft = _this.list[index].left - _this.contentScrollW / 2 + _this.list[index].width / 2;
_this.$emit("scrollViewClick", index)
}
}
}
</script>
<style lang="scss">
scroll-view ::-webkit-scrollbar {
display: none;
}
.center-cut-menu {
width: 100%;
height: 100rpx;
box-sizing: border-box;
.scroll-view {
height: 100rpx;
white-space: nowrap;
.scroll-item {
height: 100rpx;
padding: 0 20rpx;
display: inline-block;
text-align: center;
.item-text {
font-size: 30rpx;
line-height: 100rpx;
padding-bottom: 10rpx;
&.active {
color: #1468FF;
border-bottom: 5rpx solid #1468FF;
}
}
}
}
}
</style>
3. File placement path :

4. Pass the clicked position to the parent component :

Two 、 Parent component
1. Custom component reference and registration

2. Custom components use

3. The overall code of the parent component :
<template>
<view class="content">
<hdScrollview :list="list" @scrollViewClick="scrollViewClick"></hdScrollview>
</view>
</template>
<script>
import hdScrollview from "../../components/HuaDongScrollviewCenter.vue"
export default {
components: {
hdScrollview
},
data() {
return {
list: [{
id: 1,
name: ' Monday '
},
{
id: 2,
name: ' Tuesday '
},
{
id: 3,
name: ' Wednesday '
},
{
id: 4,
name: ' Thursday '
},
{
id: 5,
name: ' Friday '
},
{
id: 6,
name: ' Saturday '
},
{
id: 7,
name: ' Sunday '
},
{
id: 8,
name: ' Tuesday '
},
{
id: 9,
name: ' Saturday '
},
{
id: 10,
name: ' 星期十 '
}
],
}
},
methods: {
scrollViewClick: function(index) {
var _this = this
console.log(" Selected location index = " + index)
console.log(" Select content content = " + _this.list[index].name)
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
background-color: white;
}
page {
background-color: white;
}
</style>
Same article :uni-app Use scrollview+swiper Realize the function of sliding and switching pages
边栏推荐
- Is the compass app regular? Is it safe or not
- js实现文字跑马灯效果
- todolist未完成,已完成
- Types and application methods of screen printing
- 2022.6.23-----leetcode.30
- Solve the problem that the uniapp plug-in Robin editor reports an error when setting the font color and background color
- Cultivate children's creativity under the concept of project steam Education
- kotlin快速上手
- Various errors in kitti2bag installation
- Classic model – RESNET
猜你喜欢

请求对象,发送请求

解决uniapp插件robin-editor设置字体颜色和背景颜色报错的问题

Analysis on the diversification of maker space mechanism construction

小米电视的网页和珠宝的网页

Qt 中 deleteLater 使用总结

点击事件

How Inkscape converts PNG pictures to SVG pictures without distortion

Request object, send request

Using meta analysis to drive the development of educational robot

QT compilation error: unknown module (s) in qt: script
随机推荐
redux-thunk 简单案例,优缺点和思考
The role of children's programming in promoting traditional disciplines in China
An easy-to-use tablayout
Is it safe to open a fund account? How to apply
请求对象,发送请求
usb peripheral 驱动 - 枚举
Sqlitestudio download address
数字孪生智慧水务,突破海绵城市发展困境
2022.6.24-----leetcode.515
todolist未完成,已完成
进程之间的通信方式
优化——多目标规划
面试阿里测开岗失败后,被面试官在朋友圈吐槽了......(心塞)
General operations of asynctask
Dynamic segment tree leetcode seven hundred and fifteen
【LOJ#6718】九个太阳「弱」化版(循环卷积,任意模数NTT)
Deletelater Usage Summary in QT
Plug in installation and shortcut keys of jupyter notebook
Is the compass app regular? Is it safe or not
[paper notes] learning to grasp with primitive shaped object policies