uni-app条件动态绑定样式

迈不过友情╰ 2022-12-17 07:08 230阅读 0赞

首先我们需要了解实现什么效果,

这里我想实现的效果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQyNTQzMjY0_size_16_color_FFFFFF_t_70

图片是否显示, 因为图片是绝对定位, 文本需要缩进行,这里动态绑定是否缩进行

直接看代码吧,不难

  1. <template>
  2. <view class="yh-list">
  3. <!--顶部搜索导航栏 S -->
  4. <uni-nav-bar-classify class="yh-list-nav" background-color="#FFFFFF">
  5. <view class="nav-left" slot="left" @click="goBack">
  6. <image class="go-back" :src="iconGoBack" mode="widthFix"></image>
  7. </view>
  8. <view class="input-box-right">
  9. <input class="text-input" type="text" :value="hotSearchDefault || '请输入关键字'" />
  10. <image class="icon-search" src="@/static/icon/search.png" mode="widthFix"></image>
  11. </view>
  12. </uni-nav-bar-classify>
  13. <!--顶部搜索导航栏 E -->
  14. <!-- 写入内容 S -->
  15. <view class="comment">
  16. <!-- 顶部导航 S -->
  17. <view class="top-nav">
  18. <view class="menu">
  19. <block v-for="(item, index) in goodsMenuTop" , :key="index">
  20. <view class="menu-list">
  21. <view class="left">
  22. {
  23. {item.title}}
  24. </view>
  25. <block v-if="index == 0">
  26. <image class="select-image" src="../../../static/icon/icon-select-close.png" mode="widthFix"></image>
  27. </block>
  28. <block v-if="index == 2">
  29. <image class="select-image" src="../../../static/icon/icon-select-close.png" mode="widthFix"></image>
  30. </block>
  31. </view>
  32. </block>
  33. <view class="layout-cut" @click="chooseLayoutCut">
  34. <image class="layout-cut-image" src="../../../static/icon/icon-list.png" mode="widthFix"></image>
  35. </view>
  36. </view>
  37. <view class="goods-filtrate">
  38. <view class="shaixuan">
  39. 筛选
  40. </view>
  41. <image class="filtrate-image" src="../../../static/icon/icon-shaixuan.png" mode="widthFix"></image>
  42. </view>
  43. </view>
  44. <!-- 顶部导航 E -->
  45. <block v-if="layoutState != false">
  46. <!-- 四宫格布局 S -->
  47. <view class="grid-layout">
  48. 1
  49. </view>
  50. <!-- 四宫格布局 E -->
  51. </block>
  52. <block v-else>
  53. <!-- 列表布局 S -->
  54. <view class="list-layout">
  55. <view class="goods" v-for="(item, index) in goodsList" :key>
  56. <view class="goods-cover">
  57. <image class="cover-image" :src="item.goods_cover || errorImage" mode=""></image>
  58. </view>
  59. <view class="goods-info">
  60. <view class="goods-name">
  61. <block v-if="item.is_activity != 0">
  62. <image class="goods-activity" src="../../../static/banner/1.jpg" mode=""></image>
  63. </block>
  64. <text class="name" :class="{'text-indent': (item.is_activity != 0)}">{
  65. {item.goods_name}}</text>
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. <!-- 列表布局 E -->
  71. </block>
  72. </view>
  73. <!-- 写入内容 E -->
  74. </view>
  75. </template>
  76. <script>
  77. import uniNavBarClassify from '@/components/uni-nav-bar-classify/uni-nav-bar-classify.vue'
  78. export default {
  79. components: {
  80. uniNavBarClassify
  81. },
  82. data() {
  83. return {
  84. iconGoBack: this.$mAssetsPath.iconGoBack,
  85. errorImage: this.$mAssetsPath.errorImage,
  86. hotSearchDefault: '请输入关键字', // 搜索默认关键字
  87. goodsMenuTop: this.$mConstDataConfig.goodsMenuTop, // 商品列表导航菜单
  88. layoutState: false, // 显示布局状态默认false
  89. goodsList: [
  90. {
  91. goods_name: "高清镜面, 智能屏显, 9D音效高清镜面, 智能屏显, 9D音效高清镜面, 智能屏显, 9D音效",
  92. goods_cover: "",
  93. goods_price: 78.00,
  94. goods_comment_num: 1000,
  95. goods_good_reputation: 93,
  96. shop_name: "惠多多专营店",
  97. is_advertising: 0,
  98. is_activity: 1,
  99. is_autotrophy: 0,
  100. },
  101. {
  102. goods_name: "高清镜面, 智能屏显, 9D音效高清镜面, 智能屏显, 9D音效高清镜面, 智能屏显, 9D音效",
  103. goods_cover: "",
  104. goods_price: 78.00,
  105. goods_comment_num: 1000,
  106. goods_good_reputation: 93,
  107. shop_name: "惠多多专营店",
  108. is_advertising: 0,
  109. is_activity: 0,
  110. is_autotrophy: 0,
  111. }
  112. ]
  113. }
  114. },
  115. methods: {
  116. // 返回上一级事件
  117. goBack() {
  118. uni.navigateBack({
  119. delta: 1
  120. })
  121. },
  122. // 布局切换
  123. chooseLayoutCut() {
  124. this.layoutState = !this.layoutState
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss">
  130. .yh-list {
  131. width: 100%;
  132. background-color: #fff;
  133. display: flex;
  134. background-color: $color-white;
  135. /* #ifdef APP-PLUS */
  136. margin-top: calc(20upx + var(--status-bar-height));
  137. /* #endif */
  138. .yh-list-nav {
  139. position: fixed;
  140. z-index: 99;
  141. top: 0;
  142. .nav-left {
  143. width: 30px;
  144. padding-top: 6px;
  145. .go-back {
  146. width: 20px;
  147. }
  148. }
  149. .input-box-right {
  150. width: 100%;
  151. background-color: #f8f8f8;
  152. display: flex;
  153. align-items: center;
  154. height: 28px;
  155. border-radius: 40upx;
  156. padding: 0 20upx 0 30upx;
  157. .icon-search {
  158. width: 22px;
  159. }
  160. .text-input {
  161. width: 100%;
  162. font-size: 14px;
  163. color: #909399;
  164. }
  165. }
  166. }
  167. ,
  168. // 导航菜单
  169. .comment {
  170. width: 100%;
  171. background-color: #f8f8f8;
  172. // display: flex;
  173. height: 100%;
  174. position: absolute;
  175. padding-top: 88upx;
  176. /* #ifdef APP-PLUS */
  177. padding-top: calc(88upx + var(--status-bar-height));
  178. /* #endif */
  179. bottom: 0;
  180. .top-nav {
  181. width: 100%;
  182. height: 80upx;
  183. background-color: #FFFFFF;
  184. display: flex;
  185. align-items: center;
  186. font-size: $font-sm;
  187. box-shadow: 0 1upx 5upx #EEEEEE;
  188. .menu {
  189. width: 80%;
  190. display: flex;
  191. align-items: center;
  192. justify-content: space-between;
  193. .menu-list:nth-child(1),
  194. .menu-list:nth-child(3) {
  195. width: 180upx;
  196. height: 50upx;
  197. }
  198. .menu-list:nth-child(3) {
  199. justify-content: center;
  200. }
  201. .menu-list {
  202. display: flex;
  203. align-items: center;
  204. justify-content: center;
  205. .select-image {
  206. width: 35upx;
  207. }
  208. }
  209. .layout-cut {
  210. display: flex;
  211. align-items: center;
  212. margin-right: 28upx;
  213. }
  214. .layout-cut image {
  215. width: 40upx;
  216. }
  217. }
  218. .goods-filtrate {
  219. width: 20%;
  220. display: flex;
  221. align-items: center;
  222. justify-content: center;
  223. border-left: 1rpx solid #EEEEEE;
  224. height: 30upx;
  225. .shaixuan {}
  226. .filtrate-image {
  227. width: 30upx;
  228. margin-left: 5upx;
  229. }
  230. }
  231. }
  232. // 商品列表布局
  233. .list-layout {
  234. padding: 20rpx;
  235. .goods {
  236. display: flex;
  237. justify-content: space-between;
  238. .goods-cover {
  239. width: 300rpx;
  240. height: 300rpx;
  241. .cover-image {
  242. width: 100%;
  243. height: 100%;
  244. }
  245. }
  246. .goods-info {
  247. width: 392rpx;
  248. .goods-name {
  249. font-size: $font-base;
  250. .goods-activity {
  251. width: 50rpx;
  252. height: 28rpx;
  253. border-radius: 5rpx;
  254. margin-top: 6rpx;
  255. margin-right: 20rpx;
  256. vertical-align: middle;
  257. position: absolute;
  258. }
  259. .text-indent {
  260. text-indent: 70rpx
  261. }
  262. .name {
  263. text-overflow: -o-ellipsis-lastline;
  264. overflow: hidden;
  265. text-overflow: ellipsis;
  266. display: -webkit-box;
  267. -webkit-line-clamp: 2;
  268. line-clamp: 2;
  269. -webkit-box-orient: vertical;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }
  276. }
  277. </style>

判断样式主要代码 :class=”{‘text-indent’: (item.is_activity != 0)}“

有什么疑问可下方留言, 如有更好的解决办法,请下方留言,共同学习,谢谢大家支持

发表评论

表情:
评论列表 (有 0 条评论,230人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue 动态样式

    1. 简介 本小节我们将介绍 Vue 中如何动态绑定样式。包括 Class 的绑定、内联样式 Style 的绑定。掌握样式绑定的多种形式是其中的重点难点。同学们可以在学完

    相关 VUE 样式

    操作元素的 class 列表和内联样式是数据绑定的一个常见需求。因为它们都是属性,所以我们可以用 v-bind 处理它们:只需要通过表达式计算出字符串结果即可。不过,字符串拼接