general-attribute.uvue 4.2 KB
Newer Older
1 2 3 4 5 6
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
    <!-- #endif -->
    <view>
      <page-head :title="title"></page-head>
A
Anne_LXM 已提交
7
      <view class="uni-padding-wrap">
DCloud-WZF's avatar
DCloud-WZF 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
        <view
          :id="generalId"
          :class="generalClass"
          :name="generalName"
          :title="generalTitle"
          :data-test="generalData"
          :style="generalStyle"
          ref="general-target"
        >
          <text>id: {{ generalId }}</text>
          <text>class: {{ generalClass }}</text>
          <text>name: {{ generalName }}</text>
          <text>title: {{ generalTitle }}</text>
          <text>data-test: {{ generalData }}</text>
          <text>style: {{ generalStyle }}</text>
        </view>
        <view
          class="btn btn-style uni-common-mt"
          @click="validateGeneralAttributes"
        >
28 29
          <text class="btn-inner">{{ validateGeneralAttrText }}</text>
        </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
30
        <view class="btn btn-ref uni-common-mt" @click="changeHeight">
DCloud-WZF's avatar
DCloud-WZF 已提交
31
          <text class="btn-inner">{{changeHeightByRefText}}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
32
        </view>
33
        <view class="view-class" :hover-class="hoverClass" ref="view-target">
DCloud-WZF's avatar
DCloud-WZF 已提交
34 35
          <text class="text">按下 50 ms 后背景变红</text>
          <text class="text">抬起 400 ms 后背景恢复</text>
36
        </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
37 38 39 40 41 42 43
        <view
          class="view-class"
          :hover-class="hoverClass"
          :hover-start-time="1000"
          :hover-stay-time="1000"
          ref="view-target"
        >
DCloud-WZF's avatar
DCloud-WZF 已提交
44 45
          <text class="text">按下 1000 ms 后背景变红</text>
          <text class="text">抬起 1000 ms 后背景恢复</text>
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        </view>
      </view>
    </view>
    <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts">
export default {
  data() {
    return {
      title: 'general-attribute',
      generalId: 'general-id',
      generalClass: 'general-class',
      generalName: 'general-name',
      generalTitle: 'general-title',
      generalData: 'general-data',
DCloud-WZF's avatar
DCloud-WZF 已提交
64
      generalStyle: 'background-color: aqua',
65 66 67
      validateGeneralAttrText: '验证基础属性',
      hoverClass: 'hover-class',
      validateViewAttrText: '验证 view 属性',
DCloud-WZF's avatar
DCloud-WZF 已提交
68
      changeHeightByRefText: '通过 ref 修改高度',
69 70 71 72
    }
  },
  methods: {
    validateGeneralAttributes() {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
73
      const generalTarget = this.$refs['general-target'] as UniElement
74 75 76 77 78
      const generalId = generalTarget.getAttribute('id')
      if (generalId != this.generalId) {
        this.validateGeneralAttrText = '基础属性 id 验证失败'
        return
      }
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
79
      // #ifdef APP
80
      if (!generalTarget.classList.includes('general-class')) {
DCloud-WZF's avatar
DCloud-WZF 已提交
81 82 83 84
      // #endif
      // #ifdef WEB
      if (!Array.from(generalTarget.classList).includes('general-class')) {
      // #endif
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        this.validateGeneralAttrText = '基础属性 class 验证失败'
        return
      }
      const generalName = generalTarget.getAttribute('name')
      if (generalName != this.generalName) {
        this.validateGeneralAttrText = '基础属性 name 验证失败'
        return
      }
      const generalTitle = generalTarget.getAttribute('title')
      if (generalTitle != this.generalTitle) {
        this.validateGeneralAttrText = '基础属性 title 验证失败'
        return
      }
      const generalData = generalTarget.getAttribute('data-test')
      if (generalData != this.generalData) {
        this.validateGeneralAttrText = '基础属性 data-test 验证失败'
        return
      }
      this.validateGeneralAttrText = '基础属性验证成功'
    },
DCloud-WZF's avatar
DCloud-WZF 已提交
105
    changeHeight(){
DCloud-yyl's avatar
DCloud-yyl 已提交
106
      const generalTarget = this.$refs['general-target'] as UniElement
DCloud-WZF's avatar
DCloud-WZF 已提交
107
      this.changeHeightByRefText = '已通过 ref 修改高度'
DCloud-yyl's avatar
DCloud-yyl 已提交
108
      generalTarget.style.setProperty('height', '200px')
DCloud-WZF's avatar
DCloud-WZF 已提交
109
    }
110 111 112 113 114
  },
}
</script>

<style>
115 116 117 118 119 120 121 122 123 124 125
.btn {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #409eff;
  border-radius: 5px;
}
.btn-inner {
  color: #fff;
}
126
.general-class {
DCloud-WZF's avatar
DCloud-WZF 已提交
127 128 129 130
  margin-left: 40px;
  padding: 10px;
  width: 260px;
  height: 160px;
131 132 133 134 135 136 137 138 139
  background-color: antiquewhite;
}
.view-class {
  margin: 20px 0 0 50px;
  padding: 10px;
  width: 240px;
  height: 100px;
  background-color: antiquewhite;
}
DCloud-WZF's avatar
DCloud-WZF 已提交
140
.view-class .text {
141
  margin-top: 5px;
142 143 144 145 146 147
  text-align: center;
}
.hover-class {
  background-color: red;
}
</style>