移动端tab的样式改变

第一种:通过使用JS来增加或删除类名来达到可以切换tab的时候样式也跟着改变的效果!主要是增加和删除"active"这个类名,当选中一个tab的时候,这个tab的类名就多了“active”,其他tab没有

HTML:


    <!-- bottom -->
    <ul id="bottom">
         <a href="index.html" class="icon active">
                <li>
                    <i class="1"></i>
                    <span>1</span>
                </li>
          </a>

          <a href="#" class="2">
                <li>
                    <i class="chat"></i>
                    <span>2</span>
                </li>
         </a>

         <a href="#" class="icon">
                <li>
                    <i class="3"></i>
                    <span>3</span>
                </li>
         </a>
    </ul>

 

/* bottom tab的整体样式*/

#bottom {
    width: 100%;
    height: ;
    background-color: rgba(, , , );
    position: fixed;
    bottom: 0;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    padding: ;
}


/* 没有选中之前的样式 */
.icon li {
    color: #000000;
    font-size: ;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
}
/*没有选中之前的icon样式*/
.1:before {
    width: ;
    height: ;
    display: block;
    content: '';
    margin: 0 auto;
    background-image: url(../icon/home_normal.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
}

.2:before {
    width: ;
    height: ;
    display: block;
    content: '';
    margin: 0 auto;
    background-image: url(2_normal.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
}

.3:before {
    width: ;
    height: ;
    display: block;
    content: '';
    margin: 0 auto;
    background-image: url(3_normal.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
}



/* 选中之后 */
.active li{
    color: #ffffff;
    font-size: 0.1rem;
}
/*选中之后的icon样式*/
.active .home:before{
    background-image: url(1_press.png);
}
.active .chat:before{
    background-image: url(2_press.png);
}
.active .mine:before{
    background-image: url(3_press.png);
}

JS:

// tab切换样式
window.onload = function () {
    //根据类名获取全部按钮
    var arr = document.getElementsByClassName('icon');
    for (var i = 0; i < arr.length; i++) {
        //给全部按钮添加onclick事件
        arr[i].onclick = function () {
            console.log('点击' + this.id);
            //1.首先把所有按钮的选中状态去掉
            for (var j = 0; j < arr.length; j++) {
                arr[j].classList.remove('active');
            }
            //2.this是当前激活的按钮,为当前激活的按钮添加选中状态类名
            this.classList.add('active');
        }
    }
}

 

第二种:在vue文件中实现导航栏切换时,样式也随之改变

<template>
    <div>
        <div class="inquiryType">
			<span class="title">问诊类型</span>
			<ul class="items">
				<li @click="clickHandle(1)" :class="currentType==1 ? 'on' :'notOn' ">病情咨询</li>
				<li @click="clickHandle(2)" :class="currentType==2 ? 'on' :'notOn' ">挂号咨询</li>
				<li @click="clickHandle(3)" :class="currentType==3 ? 'on' :'notOn' ">用药建议</li>
				<li @click="clickHandle(4)" :class="currentType==4 ? 'on' :'notOn' ">诊后指导</li>
			</ul>
		</div>

<style scoped>
/*问诊类型*/
	.items {list-style: none; display: flex; justify-content: space-between; padding: 0; }
	/*没有选中的样式*/
	.items li {display: flex; align-items: center; justify-content: center;  border-radius: 2px; border: 1px solid none; line-height: 1;  width: 80px; height: 30px;}
	.notOn {color: #999999; font-size: 12px; background-color: #f2f2f2;}
    /*选中样式*/
	.on {background-color: #3091f2; font-size: 12px; line-height: 1.25; color: #ffffff; }
</style>

<script>
	export default{
		name: '',
		data() {
			return{
				currentType: 0
			}
		},
		mounted() {},
        //主要代码
		methods:{
			clickHandle (num) {
				this.currentType = num;
			}
		},
		beforeRouteLeave(to, form, next) {}
	}
</script>

 

[removed] $(function() { function setCurrentSlide(ele, index) { $(".swiper1 .swiper-slide").removeClass("selected"); ele.addClass("selected"); //swiper1.initialSlide=index; } var swiper1 = new Swiper('.swiper1', { // 设置slider容器能够同时显示的slides数量(carousel模式)。 // 可以设置为number或者 'auto'则自动根据slides的宽度来设定数量。 // loop模式下如果设置为'auto'还需要设置另外一个参数loopedSlides。 slidesPerView: 5.5, paginationClickable: true,//此参数设置为true时,点击分页器的指示点分页器会控制Swiper切换。 spaceBetween: 10,//slide之间的距离(单位px)。 freeMode: true,//默认为false,普通模式:slide滑动时只滑动一格,并自动贴合wrapper,设置为true则变为free模式,slide会根据惯性滑动且不会贴合。 loop: false,//是否可循环 onTab: function(swiper) { var n = swiper1.clickedIndex; } }); swiper1.slides.each(function(index, val) { var ele = $(this); ele.on("click", function() { setCurrentSlide(ele, index); swiper2.slideTo(index, 500, false); //mySwiper.initialSlide=index; }); }); var swiper2 = new Swiper('.swiper2', { //freeModeSticky 设置为true 滑动会自动贴合 direction: 'horizontal',//Slides的滑动方向,可设置水平(horizontal)或垂直(vertical)。 loop: false, // effect : 'fade',//淡入 //effect : 'cube',//方块 //effect : 'coverflow',//3D流 // effect : 'flip',//3D翻转 autoHeight: true,//自动高度。设置为true时,wrapper和container会随着当前slide的高度而发生变化。 onSlideChangeEnd: function(swiper) { //回调函数,swiper从一个slide过渡到另一个slide结束时执行。 var n = swiper.activeIndex; setCurrentSlide($(".swiper1 .swiper-slide").eq(n), n); swiper1.slideTo(n, 500, false); } }); }); [removed]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值