uni-app :左右滑动 swiper

官方文档 : https://uniapp.dcloud.io/component/swiper?id=swiper

<template>
	<view>
		<view class="nav">
			<view :class="current== 0 ? 'skyblue' : '' " @tap="onnav(0)">安装信息</view>
			<view :class="current== 1 ? 'skyblue' : '' " @tap="onnav(1)">OBD数据</view>
			<view :class="current== 1 ? 'span spanright' : 'span '"></view>
		</view>
		
		// current  : 当前所在滑块的 index	
		// @change 事件 : current 改变时会触发 change 事件,event.detail = {current: current, source: source}	
		
		<swiper :current="current" @change="onchange">
			<swiper-item>
				<view>
					第一个页面
				</view>
			</swiper-item>
			<swiper-item>
				<view>
					第二个页面
				</view>
			</swiper-item>
		</swiper>
	</view>

</template>
<script>
	export default {
		data(){
			return {
				current:0,
			}
		},
		methods:{
			onnav(index) {
				this.current = index;
			},
			onchange(e){
				this.current = e.detail.current;
			}
			
		}

	}
</script>
<style lang="scss">
	.nav {
		position: relative;
		border-bottom: 1px solid #dddddd;
		&::after {
			display: block;
			content: "";
			height: 0;
			line-height: 0;
			visibility: hidden;
			clear: both;
		}
		view {
			float: left;
			width: 50%;
			text-align: center;
			padding: 30rpx 0rpx;
		}
		
		.span {
			width: 50%;
			position: absolute;
			padding: 4rpx 0rpx;
			background-color: #2598e4;
			bottom: 0;
			left: 0;
			transition: ease-in-out .2s;
			transform: translateY(100%);
		}
		.spanright {
			left: 50%;
		}
		.skyblue {
			color: #2598e4;
		}
	}
</style>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在React函数组件中,我们可以使用React Hooks来替代componentDidMount()生命周期方法。同时,可以使用useState来管理组件的状态。下面是修改后的代码: ```javascript import React, { useEffect, useState } from 'react'; function MyComponent() { const [appAuthLength, setAppAuthLength] = useState(0); const [userinfo, setUserinfo] = useState(null); useEffect(() => { const fetchData = async () => { const data = JSON.parse(sessionStorage.getItem('retstring')); setUserinfo(data.userinfo[0]); setAppAuthLength(data.app_auth_length); }; fetchData(); }, []); useEffect(() => { const swiperOptions = { loop: false, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, slidesPerView: 'auto', spaceBetween: 80, centeredSlides: true, loopedSlides: 9, }; if (userinfo.role === '财务') { swiperOptions.loop = false; } else if (appAuthLength > 2) { swiperOptions.loop = true; } new Swiper('.swiper-container', swiperOptions); }, [userinfo.role, appAuthLength]); return ( <div className="swiper-container"> {/* Swiper slides */} </div> ); } export default MyComponent; ``` 在上面的代码中,我们使用了两个useEffect钩子函数。第一个useEffect用来获取并设置初始状态,模拟componentDidMount的功能。第二个useEffect用来监听状态的变化,并根据条件设置Swiper的选项。最后,将Swiper组件渲染到页面中。 请注意,为了正确使用Hooks,我们需要将代码封装在函数组件中,并使用`import React, { useEffect, useState } from 'react';`引入React和Hooks。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值