uniapp开发小程序—scroll-view实现内容滚动时, 标题也滚动

这篇具有很好参考价值的文章主要介绍了uniapp开发小程序—scroll-view实现内容滚动时, 标题也滚动。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、需求

scroll-view实现内容滚动时, 标题也滚动

二、效果

uniapp开发小程序—scroll-view实现内容滚动时, 标题也滚动,uniapp,uni-app,小程序,前端

三、代码实现

<template>
	<view class="content">
		<view class="head">头部固定区域</view>
		<view class="list_box">
			<!-- 菜单左边 -->
			<view class="left">
				<scroll-view scroll-y="true" class="scroll">
					<view class="item" v-for="(item,index) in leftArray" :key="index"
						:class="{ 'active':index==leftIndex }" :data-index="index" @tap="leftTap">{{item.id}}</view>
				</scroll-view>
			</view>
			<!-- 右侧内容部分 -->
			<view class="main">
				<scroll-view scroll-y="true" @scroll="mainScroll" class="scroll" :scroll-into-view="scrollInto"
					:scroll-with-animation="true" @touchstart="mainTouch" id="scroll-el">
					<block v-for="(item,index) in mainArray" :key="index">
						<scroll-view class="right-scroll"  :id="'item-'+index">
							<!-- :scroll-x="true" 加上可以横向滑动 -->
							<block v-for="(item2,index2) in item.list" :key="index2">
								<view class="item">
									<view class="goods">
										<view>左边是第{{ index + 1 }}</view>
										<view>右边是第{{ index2+1 }}</view>
									</view>
								</view>
							</block>
						</scroll-view>
					</block>

				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				leftArray: [{
						id: 1
					},
					{
						id: 2
					},
					{
						id: 3
					},
					{
						id: 4
					},
					{
						id: 5
					},
					{
						id: 6
					},
					{
						id: 7
					},
					{
						id: 8
					},
				],
				mainArray: [],
				topArr: [],
				leftIndex: 0,
				isMainScroll: false,
				scrollInto: ''
			}
		},
		mounted() {
			this.getListData();
		},
		methods: {
			/* 获取列表数据 */
			getListData() {
				/* 因无真实数据,当前方法模拟数据 */
				let [left, main] = [
					[],
					[]
				];

				for (let i = 0; i < 8; i++) {
					left.push(`${i+1}类商品`);

					let list = [];
					for (let j = 0; j < (i + 1); j++) {
						list.push(j);
					}
					main.push({
						title: `第${i+1}类商品标题`,
						list
					})
				}
				this.mainArray = main;

				this.$nextTick(() => {
					setTimeout(() => {
						this.getElementTop();
					}, 10)
				});
			},
			//获取距离顶部的高度
			getScrollTop(selector) {
				return new Promise((resolve, reject) => {
					let query = uni.createSelectorQuery().in(this);
					query.select(selector).boundingClientRect(data => {
						resolve(data.top)
					}).exec();
				})
			},
			/* 获取元素顶部信息 */
			async getElementTop() {
				/* Promise 对象数组 */
				let p_arr = [];

				/* 遍历数据,创建相应的 Promise 数组数据 */
				for (let i = 0; i < this.mainArray.length; i++) {
					const resu = await this.getScrollTop(`#item-${i}`)
					p_arr.push(resu - 200)
				}

				/* 主区域滚动容器的顶部距离 */
				this.getScrollTop("#scroll-el").then((res) => {
					let top = res;
					// #ifdef H5
					top += 43; //因固定提示块的需求,H5的默认标题栏是44px
					// #endif

					/* 所有节点信息返回后调用该方法 */
					Promise.all(p_arr).then((data) => {
						this.topArr = data;
					});
				})
			},

			/* 主区域滚动监听 */
			mainScroll(e) {
				if (!this.isMainScroll) {
					return;
				}
				let top = e.detail.scrollTop;
				let index = -1;
				if (top >= this.topArr[this.topArr.length - 1]) {
					index = this.topArr.length - 1;
				} else {
					index = this.topArr.findIndex((item, index) => {
						return this.topArr[index + 1] >= top;
					});
				}
				
				this.leftIndex = (index < 0 ? 0 : index);
				// console.log('打印',this.leftIndex)
			},
			/* 主区域触摸 */
			mainTouch() {
				this.isMainScroll = true;
			},
			/* 左侧导航点击 */
			leftTap(e) {
				let index = e.currentTarget.dataset.index;
				this.isMainScroll = false;
				this.leftIndex = Number(index);
				this.scrollInto = `item-${index}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		.head {
			width: 100%;
			height: 400rpx;
			background-color: pink;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		.list_box {
			display: flex;
			flex-direction: row;
			flex-wrap: nowrap;
			justify-content: flex-start;
			align-items: flex-start;
			align-content: flex-start;
			font-size: 28rpx;
			height: calc(100vh - 400rpx);

			.left {
				width: 200rpx;
				background-color: orange;
				line-height: 80rpx;
				box-sizing: border-box;
				font-size: 32rpx;
				height: 100%;

				.item {
					padding-left: 20rpx;
					position: relative;

					&:not(:first-child) {
						margin-top: 1px;

						&::after {
							content: '';
							display: block;
							height: 0;
							border-top: #d6d6d6 solid 1px;
							width: 620upx;
							position: absolute;
							top: -1px;
							right: 0;
							transform: scaleY(0.5);
						}
					}

					&.active,
					&:active {
						color: #42b983;
						background-color: #fff;
					}
				}
			}

			.main {
				height: 100%;
				background-color: #fff;
				padding: 0 20rpx;
				width: 0;
				flex-grow: 1;
				box-sizing: border-box;

				.tips {
					line-height: 64rpx;
					font-size: 24rpx;
					font-weight: bold;
					color: #666;
					height: 64rpx;
					position: fixed;
					top: 44px;
					right: 0;
					width: 530rpx;
					z-index: 10;
					background-color: #fff;
					padding-left: 10rpx;
				}

				.right-scroll {
					height: calc(100vh - 400rpx);
					width: 100%;
					background-color: #efba21;
					border-bottom: 2rpx solid #fff;
					/* 横向滚动 */
					white-space: nowrap;
					flex-direction: row;

					.item {
						width: 100%;
						height: 100%;
						/* item的外层定义成行内元素才可进行滚动 inline-block / inline-flex 均可 */
						display: inline-flex;

						.goods {
							width: 100%;
							height: 100%;
							padding: 20rpx;
							box-sizing: border-box;
							background-color: #42b983;
							display: flex;
							flex-direction: column;
							flex-wrap: nowrap;
							justify-content: center;
							align-items: center;
							align-content: center;
							margin-bottom: 10rpx;
							border-right: 2rpx solid #fff;
						}

						.goods:last-child {
							border-right: 0;
						}
					}
				}

				.right-scroll:last-child {
					border-bottom: 0;
				}
			}

			.scroll {
				height: 100%;
			}
		}
	}
</style>

完成文章来源地址https://www.toymoban.com/news/detail-804051.html

到了这里,关于uniapp开发小程序—scroll-view实现内容滚动时, 标题也滚动的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 微信小程序--开发横向滚动条scroll-view

    一、话不多说,上代码 .wxml文件写法 css样式 js写法 二、解析  scroll-x=\\\"true\\\":表明是横向滚动 class=\\\"{{currentTab==index?\\\'scroll_item\\\':\\\'\\\'}}\\\":当点击时才会变色,其中currentTab的初始值是0,通过点击事件更改值

    2024年02月12日
    浏览(34)
  • uniapp scroll-view 隐藏滚动条

     

    2024年02月14日
    浏览(29)
  • 关于uniapp微信小程序scroll-view组件使用show-scrollbar隐藏不了滚动条

    这里关于使用  scroll-view组件  时候有滚动条 想要隐藏滚动条但是使用show-scrollbar没有效果 这时候又使用类名隐藏滚动条 使用id隐藏滚动条都不行 解决方法:在使用  scroll-view组件 的页面或者app 页面加上以下代码就可以了         

    2024年02月12日
    浏览(30)
  • uniapp scroll-view横向滚动无效,scroll-view子元素flex布局不生效

    要素排查: 1.scroll-x属性需要开启,官方类型是Boolean,实际字符串也行。   2scroll-view标签需要给予一个固定宽度,可以是百分百也可以是固定宽度或者100vw。    3.子元素需要设置display: inline-block(行内块元素)属性,scroll-view需要设置white-space: nowrap(不换行) 当使用scroll

    2024年02月12日
    浏览(34)
  • uniapp中scroll-view去除滚动条

    uniapp中我们使用scroll-view实现横向或者竖向滑动时,我们会发现横向或者竖向是有一个滚动条的,不是很美观,那么我们就可以尝试去去除一下滚动条(如果采用以下官方方法无法去除时,那么请向官方反馈信息) 不过它默认是false,那就是本身不显示,可以忽略这个方法 代

    2024年02月14日
    浏览(35)
  • 【微信小程序】scroll-view滚动

    wxml文件 wxss文件       wxml文件 wxss文件     注意事项: 1、在scroll-view标签上加上样式属性: display:flex; white-space:nowrap; 2、scroll-view标签下的一级栏目标签需要加上样式属性: display: inline-block; 只有结合上面两步,才能实现横向滚动。

    2024年02月13日
    浏览(33)
  • uniapp 小程序端使用uni-popup组件时,页面用了scroll-view滚动组件,uni-popup组件也使用了scroll-view滚动出现组件滚动导致页面也滚动的解决方案

    在 uni-popup上给一个禁止滚动 @touchmove.stop.prevent=\\\"\\\" 和一个样式height: 100vh;    

    2024年02月10日
    浏览(42)
  • 微信小程序scroll-view去掉滚动条

    微信小程序scroll-view去掉滚动条 微信官方文档上面给出了显示和隐藏滚动条的属性 show-scrollbar ,把该属性设置为false,文档要求使用该属性要开启 enhanced 属性,把这个属性设置为true即可; 通过CSS设置滚动条隐藏: 值得注意的是: ::-webkit-scrollbar前要有 scroll-view 的标签名 或

    2024年02月11日
    浏览(38)
  • 小程序scroll-view组件纵向滚动返回顶部

    当开发分类页面时,常见的效果如下: 当左侧一级分类被选择之后,右侧的二级分类向下滚动后,再次点击左侧一级分类进行分类切换,二级分类应该立刻恢复到顶部。 首先,界面滚动的效果可以利用小程序中的 scroll-view 组件来实现,例如: 在确定了基本的布局后,就可以

    2024年02月11日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包