原生Vue实现点击切换代码

原生Vue实现点击切换

vue页面分为三处。

第一处是:template 用来写页面div,p,ul,li等等。

第二处是:script 用来路由跳转,点击事件方法,请求后台路径,页面加载时的数据读取等等,当然也可以写js代码,计算函数等等,不过目前还不太会用。

第三处是: style 用来设置页面样式。

实现第一处代码,代码如下

<template>
	<div>
		<div class="dhl">
			<img src="../../assets/fj3.jpg" style="height: 200px;">
			<div class="pttz" style="width: 100%;
			line-height: 50px; height: 50px; ">
				<p v-on:click="xmfClick(ShowXmf)" @click="ShowXmfchange(1)"
				:class="{ red:changeRed == 1}"
				style=" width: 38%; border-radius: 3px 3px 3px 3px;
					height: 40px;line-height: 40px; border: 1px #515A6E solid; display: inline-block;">左按钮</p>
				<p v-on:click="tzrClick(ShowTzr)" @click="ShowTzrchange(2)"
				:class="{ red:changetzr == 2}"  style="
					border-radius: 3px 3px 3px 3px;  border: 1px #515A6E solid;
					width: 38%; height: 40px;line-height: 40px; margin-left: 10px; display: inline-block;">右按钮</p>
			</div>
			<!-- <img src="../../assets/dhl.gif" width="100%" height="180px"/> -->
		</div>
	<div >
    <h1 styel='color:red' v-if="ShowXmf">我是左按钮</h1>
  </div>
	<div >
	  <h1 styel='color:red' v-if="ShowTzr">我是右按钮</h1>
  </div>
</div>
</template>

实现第一处,效果图如下(template):在这里插入图片描述

实现第二处代码,代码如下
<script>

import axios from 'axios'
export default{
	name:'add',
	data () {
	    return  {
        ShowXmf: true,
        ShowTzr: false,
        isLogin:localStorage.getItem("token"),
        path:this.$global.serverPath,
        changeRed:1,
        changetzr:1,
	    }
	},
	mounted() {},

	methods:{
		ShowXmfchange(){
			this.changeRed = 1;
			this.changetzr = 0;
		},
		ShowTzrchange(){
			this.changeRed = 0;
			this.changetzr = 2;
		},
		// 点击页面事件 隐藏需要隐藏的区域
		 xmfClick(ShowXmf){
			 if(ShowXmf == true){
				   this.ShowXmf = true;
			 }else{
				  this.ShowXmf = true;
				  this.ShowTzr = false;
			 }
		 },
     tzrClick(ShowTzr){
       ShowTzr = true;
       if(ShowTzr == true){
            this.ShowXmf = false;
          if(this.ShowXmf!=true){
            this.ShowTzr = true;
          }
       }else{
        this.ShowTzr = true;
       }
     }
	 }
}

</script>
实现第二处,按钮的点击切换就完成了,效果图如下:

在这里插入图片描述

第三处Css样式大家可以自己定义。

在此也奉上我写的Css O(∩_∩)O

<style scoped>

	.red{
		color: #FFFFFF;
		background: -webkit-gradient(linear, 0 0, 0 100%, from(#303fb2), to(#2f70d4));
	}

	body{
		background-color: #FFFFFF;
	}
	.liBackground {
	     background: -webkit-gradient(linear, 0 0, 0 100%, from(#303fb2), to(#2f70d4));
	     color: -webkit-gradient(#ffffff);
	}
</style>

实现第三处,效果图如下:

在这里插入图片描述
在这里插入图片描述

不足之处,还请大家指点,加以改正。
欢迎大家留出宝贵意见 和 评论。
一个人几乎可以在任何他怀有无限热忱的事情上成功。——查尔斯·史考伯。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2 中实现 js 原生轮播图的方法如下: 1. 在 Vue 组件中引入轮播图需要的图片,可以使用 require/import 等方式进行引入。 2. 在 Vue 组件中定义一个变量,用于存储当前轮播图的索引值。 3. 在 Vue 组件的 mounted 钩子函数中,使用 setInterval 函数定时更新当前轮播图的索引值。 4. 在 Vue 组件中使用计算属性或者 watch 监听当前轮播图的索引值,根据索引值切换轮播图。 5. 在 Vue 组件中使用 v-for 指令渲染轮播图列表,并使用 v-bind 绑定图片地址和样式。 下面是一个简单的示例代码: ```html <template> <div class="slider"> <div class="slider-container" v-bind:style="{ 'transform': 'translateX(' + (-currentIndex * 100) + '%)' }"> <div class="slider-item" v-for="(item, index) in sliderList" v-bind:key="index"> <img v-bind:src="item.url" v-bind:alt="item.title"> <div class="slider-title">{{ item.title }}</div> </div> </div> </div> </template> <script> export default { data() { return { currentIndex: 0, sliderList: [ { url: require('@/assets/images/slider-1.jpg'), title: 'Slider 1' }, { url: require('@/assets/images/slider-2.jpg'), title: 'Slider 2' }, { url: require('@/assets/images/slider-3.jpg'), title: 'Slider 3' } ] } }, mounted() { setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.sliderList.length }, 3000) } } </script> <style scoped> .slider { width: 100%; overflow: hidden; position: relative; } .slider-container { width: 300%; display: flex; transition: transform 0.5s ease-in-out; } .slider-item { width: 33.33%; display: flex; justify-content: center; align-items: center; } .slider-item img { width: 100%; } .slider-title { position: absolute; bottom: 0; left: 0; right: 0; background: rgba(0, 0, 0, 0.5); color: #fff; padding: 10px; font-size: 14px; } </style> ``` 这是一个简单的轮播图组件示例,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值