#vue# 【七】实现点击切换active样式tab栏的制作思路步骤!

news/2024/7/10 1:29:58 标签: vue, 点击事件, class, javascript, 前端
class="baidu_pl">
class="article_content clearfix">
class="markdown_views prism-atom-one-dark">

class="tags" href="/tags/VUE.html" title=vue>vue_activetab_0">#class="tags" href="/tags/VUE.html" title=vue>vue# 实现点击切换active样式&tab栏的制作思路步骤!

语法:
class={active:变量名}、@click=“方法名”、布尔值

需求:需要有当前态(就是状态的是被选中模式),
点击以后切换到另一个tab页,且加入active的样式

适用场景:tab栏、导航栏

思路:
(1)tab栏、导航栏这些一般都是差不多样式的,所以共用一个类名,不要设置宽度

(2)再写鼠标点击以后的样式(即系active)
:active表示鼠标点击状态

class="prism language-bash">.active class="token punctuation">{
    xxxxx
class="token punctuation">}

(3)在需要切换的标签处,绑定class的active,设置布尔值

class="prism language-bash">:classclass="token operator">=class="token string">"{'active':onShow}" //onShow 是一个变量名来的,赋的值是布尔值(true/false)

(4)在data-----return里面设置tab栏、导航栏的初始化状态

class="prism language-bash"> dataclass="token punctuation">(class="token punctuation">) class="token punctuation">{
        class="token keyword">return class="token punctuation">{
            onShow:true, 
            offShow:false
        class="token punctuation">}
    class="token punctuation">},

(5)绑定鼠标class="tags" href="/tags/DianJiShiJian.html" title=点击事件>点击事件,在再methods里面写入,触发事件时,标签的布尔值进行切换

class="prism language-bash">@clickclass="token operator">="getTimeclass="token punctuation">(class="token punctuation">)
 methods: class="token punctuation">{
        getTimeclass="token punctuation">(class="token punctuation">) class="token punctuation">{
           this.onShow class="token operator">= class="token boolean">trueclass="token punctuation">;
           this.offShow class="token operator">= class="token boolean">falseclass="token punctuation">;
        class="token punctuation">},

完整的demo代码如下:

class="prism language-bash">class="token operator"><templateclass="token operator">>
   class="token operator"><divclass="token operator">>
       class="token operator"><p classclass="token operator">=class="token string">"info-item" :classclass="token operator">=class="token string">"{'active':onShow}" @clickclass="token operator">=class="token string">"getTime()"class="token operator">>我是小丸子class="token operator"></pclass="token operator">>
       class="token operator"><p classclass="token operator">=class="token string">"info-item" :classclass="token operator">=class="token string">"{'active':offShow}" @clickclass="token operator">=class="token string">"getText()"class="token operator">>我是盖子盖子class="token operator"></pclass="token operator">>
   class="token operator"></divclass="token operator">>

class="token operator"></templateclass="token operator">>

class="token operator"><scriptclass="token operator">>

class="token function">export default class="token punctuation">{
    dataclass="token punctuation">(class="token punctuation">) class="token punctuation">{
        class="token keyword">return class="token punctuation">{
           //因为在初始化的时候,就已经有一个当前态样式了,
            所以在触发class="tags" href="/tags/DianJiShiJian.html" title=点击事件>点击事件以后,应该还是跟前面的布尔值保持一致,具体多试一下就知道了
            onShow:true, 
            offShow:false
        class="token punctuation">}
    class="token punctuation">},
    methods: class="token punctuation">{
        getTimeclass="token punctuation">(class="token punctuation">) class="token punctuation">{
           this.onShow class="token operator">= class="token boolean">trueclass="token punctuation">;
           this.offShow class="token operator">= class="token boolean">falseclass="token punctuation">;
        class="token punctuation">},
        getTextclass="token punctuation">(class="token punctuation">) class="token punctuation">{
            this.onShow class="token operator">= class="token boolean">falseclass="token punctuation">;
            this.offShow class="token operator">= class="token boolean">trueclass="token punctuation">;
        class="token punctuation">}


    class="token punctuation">}


class="token punctuation">}
class="token operator"></scriptclass="token operator">>

class="token operator"><style scopedclass="token operator">>
div class="token punctuation">{
    width: 10remclass="token punctuation">;
    height: 5remclass="token punctuation">;
    background-color: class="token comment">#FECC8F;
    margin: 1rem autoclass="token punctuation">;
class="token punctuation">}
.info-item class="token punctuation">{
    display: inline-blockclass="token punctuation">;
    width:2remclass="token punctuation">;
    height: 2remclass="token punctuation">;
    background-color: class="token comment">#FFFFFF;
    font-size: .2remclass="token punctuation">;
    color: class="token comment">#000000;
    margin-top: .8remclass="token punctuation">;
    margin-right: .8remclass="token punctuation">;
class="token punctuation">}
.active class="token punctuation">{
    background-color: class="token comment">#DA1A14;
class="token punctuation">}
class="token operator"></styleclass="token operator">>


http://www.niftyadmin.cn/n/615271.html

相关文章

#vue# 【八】使用iframe嵌入外部的页面到项目里

#vue#使用iframe嵌入外部的页面到项目里 一、使用场景 使用场景1&#xff1a; 如果这个外部页面是一个链接的话&#xff0c; 那就直接在需要加入的地方&#xff0c;嵌套iframe标签并指定src即可 &#xff08;绝对路径&#xff09; <iframe src"http://xxxxxx.com&qu…

# vue# 【九】 个人中心的制作

Z## vue个人中心的制作 个人中心的制作 在实际开发里面&#xff0c;经常会有一些登录页需要进行编写&#xff0c;例如下面这些类型 而这些版面的制作&#xff0c;比较讲究的是思路问题 例如我们想要制作一个登录页&#xff0c;点击就可以输入名字&#xff0c;并且修改名字 整…

#vue# 【十】如何裁剪上传并更换头像?

#vue# 如何裁剪上传并更换头像&#xff1f; 思路&#xff1a; 这个需求主要分为3个部分 &#xff08;1&#xff09;页面呈现部分 &#xff08;用一个大的div进行包裹&#xff0c;包裹头像、图片、以及最右边的部分&#xff0c;通过flex进行布局&#xff08;减少float的使用&am…

#git# 【四】 如何下载开源项目?

如何下载开源项目到本地 第一步&#xff0c;创建一个仓库&#xff08;自定义名字&#xff0c;这里为“add”&#xff09;,复制这个网址 第二步&#xff0c;在本机电脑新建一个文件夹&#xff08;例如“gitadd”&#xff09; 第三步&#xff0c;进入这个文件夹&#xff0c;右…

#css# 【四】实现鼠标移入图片显示遮罩层

css 实现鼠标移入图片显示遮罩层 实现鼠标移入图片显示遮罩层的方法有很多 比如动画库&#xff0c;动画库的使用可看 #前端开发001之Animate动画库的安装及详细用法 不过动画库使用起来还是有点麻烦&#xff0c;所以本篇分享一个原生的简洁写法 三步解决&#xff1a; 第一步…

#vue# 【十一】使用v-for循环出多个内容

#前端001之网络请求 在前端的页面中&#xff0c;网络请求是一个非常重要的存在 可以让我们省去很多排版步骤 下面就详细讲一下网络请求的步骤 首先我们排版了一个页面 比如是一个有4个相同卡片板块的页面 里面包含图片以及文字 效果示例如下 那我们只需要排版时&#xff0c;只…

#前端开发避坑技巧003# 【三】(ertical-align + inline-block、路由跳转问题、html各种分割线、鼠标点击事件、透明度、密码框表单、绑定输入框输入类型、下划线)

#前端开发避坑技巧 ertical-align inline-block、透明度、密码框表单、绑定输入框的输入类型、下划线、css继承性问题、伪类选择器、去掉边框默认样式、悬浮阴影 &#xff08;1&#xff09;如何让行内元素居中&#xff0c;一般不建议用定位以及浮动&#xff0c; 因为会造成元…

#vue# 【十二】 前端排版时的类名共用关系

#vue# 前端排版时的类名共用关系 示例&#xff0c;在一个页面里面&#xff0c; 会有很多板块都有相同类型的按钮&#xff0c; 它们的高度宽度、边框颜色都是相同的&#xff0c; 这个时候&#xff0c; 我们只需要写出一个可以复用的类名class, 只要加上这个类名&#xff0c;我们…