1、应用场景:小程序选项卡切换
html代码如下:
<view class='tabli {{current == 1 ? "active" : ""}}' data-index="1" bindtap="btnclick">
<view>申请预约</view>
<image src="../../statics/rec_tips.png"></image>
</view>
<view class='tabli {{current == 2 ? "active" : ""}}' data-index="2" bindtap="btnclick">
<view>排队信息</view>
<image src="../../statics/rec_tips.png"></image>
</view>
<view class='tabli {{current == 3 ? "active" : ""}}' data-index="3" bindtap="btnclick">
<view>预约查询</view>
<image src="../../statics/rec_tips.png"></image>
</view>
<view class="conBody" style="display:{{current == 1?'block':'none'}}">1111申请预约 代码已省略</view>
<view class="conBody" style="display:{{current == 2?'block':'none'}}">222排队信息 代码已省略</view>
<view class="conBody" style="display:{{current == 3?'block':'none'}}">333预约查询 代码已省略</view>
js代码如下:
btnclick: function (e) {
this.setData({
current: e.currentTarget.dataset.index === undefined?'':Number(e.currentTarget.dataset.index)
})
}
效果图
2、应用场景:vue项目中根据变量动态样式绑定
代码如下:
写法 :style="{left:(showMenu?’’:0),top:(showMenu?’’:‘61px’)}"
:class="{active:index==secondActive}"
<div class="main-content" :style="{left:(showMenu?'':0),top:(showMenu?'':'61px')}">
<div class="navigatorText" v-if="showMenu">{{currentNavfirstText}}/ <span style="color: #528EFC;">{{currentNavsecondText}}</span></div>
<header class="main-header-nav">
<div class="navLi" :class="{active:index==secondActive}" v-for="(item, index) in secondList" @click="secondMenuClick(index, item, 'click')">
<i v-if="item.icon != null" :class="item.icon"></i><span>{{item.name}}</span>
</div>
</header>
<section class="sectionBody" :style="{top:(showMenu?'':'12%')}" :class="{'sectionBodyActive': secondListnotNull}">
<iframe class="iframeClass" src="" frameborder="0" :src="main"></iframe>
</section>
</div>