CImage.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <div
  3. class="c-image iconfont has-bg"
  4. :class="className"
  5. :style="styles"
  6. >
  7. <template v-if="!isSnapping">
  8. <template v-if="isSlide">
  9. <transition name="slide">
  10. <div
  11. v-if="toggle"
  12. key="first"
  13. class="c-image__slider has-bg"
  14. :style="slideStyles"
  15. />
  16. <div
  17. v-else
  18. key="second"
  19. class="c-image__slider has-bg"
  20. :style="slideStyles"
  21. />
  22. </transition>
  23. </template>
  24. <template v-if="isFlip">
  25. <div
  26. class="c-image__flipper"
  27. :class="{ toggle: toggle }"
  28. >
  29. <div
  30. class="front has-bg"
  31. :style="flipFrontStyles"
  32. />
  33. <div
  34. class="back has-bg"
  35. :style="flipBackStyles"
  36. />
  37. </div>
  38. </template>
  39. <template v-if="isSectionFlip">
  40. <div
  41. v-for="(sectionStyle, index) in sectionStyles"
  42. :key="index"
  43. class="c-image__flipper"
  44. :class="{ toggle: toggle }"
  45. >
  46. <div
  47. class="front"
  48. :style="[flipFrontStyles, sectionStyle]"
  49. />
  50. <div
  51. class="back"
  52. :style="[flipBackStyles, sectionStyle]"
  53. />
  54. </div>
  55. </template>
  56. </template>
  57. </div>
  58. </template>
  59. <script>
  60. import { getThumbnailUrl } from '@/api/asset'
  61. import { WidgetType } from '../constant'
  62. import { switchToNext } from '../utils'
  63. export default {
  64. name: WidgetType.IMAGE,
  65. inject: ['control'],
  66. props: {
  67. node: {
  68. type: Object,
  69. default: null
  70. }
  71. },
  72. data () {
  73. return {
  74. img: null,
  75. frontImage: null,
  76. backImage: null,
  77. toggle: false
  78. }
  79. },
  80. computed: {
  81. isSnapping () {
  82. return this.control.snapping
  83. },
  84. styles () {
  85. const {
  86. width,
  87. height,
  88. radius
  89. } = this.node
  90. return {
  91. width: `${width}px`,
  92. height: `${height}px`,
  93. 'font-size': `${Math.min(width, height) / 3 | 0}px`,
  94. 'border-radius': `${radius}px`,
  95. 'background-image': !this.isEmpty && this.isNormal ? `url(${getThumbnailUrl(this.img)})` : 'none'
  96. }
  97. },
  98. className () {
  99. return this.isSnapping ? {
  100. empty: this.isEmpty
  101. } : {
  102. empty: this.isEmpty,
  103. slide: this.isSlide,
  104. fade: this.isFade,
  105. flip: this.isFlip || this.isSectionFlip
  106. }
  107. },
  108. isEmpty () {
  109. return !this.img
  110. },
  111. slideStyles () {
  112. return {
  113. 'background-image': this.img ? `url(${getThumbnailUrl(this.img)})` : 'none'
  114. }
  115. },
  116. flipFrontStyles () {
  117. return {
  118. 'background-image': this.frontImage ? `url(${getThumbnailUrl(this.frontImage)})` : 'none'
  119. }
  120. },
  121. flipBackStyles () {
  122. return {
  123. 'background-image': this.backImage ? `url(${getThumbnailUrl(this.backImage)})` : 'none'
  124. }
  125. },
  126. isNormal () {
  127. const { animation } = this.node
  128. return this.isSnapping || animation === 'none' || animation === 'fade'
  129. },
  130. isFade () {
  131. return this.node.animation === 'fade'
  132. },
  133. isSlide () {
  134. return this.node.animation === 'slide'
  135. },
  136. isFlip () {
  137. return this.node.animation === 'flip'
  138. },
  139. isSectionFlip () {
  140. return this.node.animation === 'section-flip'
  141. },
  142. sctionWidth () {
  143. return this.width / 10
  144. },
  145. sectionStyles () {
  146. const { width } = this.node
  147. const offset = width / 10
  148. const styles = []
  149. for (let i = 0; i < 10; i++) {
  150. styles.push({
  151. 'background-size': `${width}px 100%`,
  152. 'background-position-x': `-${offset * i}px`
  153. })
  154. }
  155. return styles
  156. }
  157. },
  158. watch: {
  159. 'node.sources' () {
  160. this.reset()
  161. },
  162. 'node.interval' (val) {
  163. if (this.node.sources.length > 1) {
  164. clearTimeout(this.$timer)
  165. this.$timer = setTimeout(this.toggleImage, val * 1000)
  166. }
  167. },
  168. isSnapping (val) {
  169. if (this.node.sources.length > 1) {
  170. if (val) {
  171. clearTimeout(this.$timer)
  172. } else {
  173. this.$timer = setTimeout(this.toggleImage, val * 1000)
  174. }
  175. }
  176. }
  177. },
  178. created () {
  179. this.$timer = -1
  180. this.reset()
  181. },
  182. beforeDestroy () {
  183. clearTimeout(this.$timer)
  184. },
  185. methods: {
  186. reset () {
  187. this.$index = -1
  188. this.choose()
  189. this.changeFlip()
  190. },
  191. changeFlip () {
  192. if (this.toggle) {
  193. this.backImage = this.img
  194. } else {
  195. this.frontImage = this.img
  196. }
  197. },
  198. toggleImage () {
  199. this.choose()
  200. this.toggle = !this.toggle
  201. this.changeFlip()
  202. },
  203. choose () {
  204. const { sources, interval, toggleType } = this.node
  205. clearTimeout(this.$timer)
  206. this.$index = switchToNext(toggleType, sources, this.$index)
  207. this.img = sources[this.$index]
  208. if (sources.length > 1) {
  209. this.$timer = setTimeout(this.toggleImage, interval * 1000)
  210. }
  211. }
  212. }
  213. }
  214. </script>
  215. <style lang="scss" scoped>
  216. .c-image {
  217. position: relative;
  218. display: inline-block;
  219. overflow: hidden;
  220. &.empty {
  221. display: inline-flex;
  222. justify-content: center;
  223. align-items: center;
  224. background-color: rgba(255, 255, 255, 0.8);
  225. &::before {
  226. content: "\e612";
  227. font-size: inherit;
  228. }
  229. }
  230. &.fade {
  231. transition: background-image 1s;
  232. }
  233. &.slide {
  234. .slide-enter {
  235. transform: translateX(100%);
  236. }
  237. .slide-leave-to {
  238. transform: translateX(-100%);
  239. }
  240. .c-image__slider {
  241. position: absolute;
  242. width: 100%;
  243. height: 100%;
  244. transition: transform 0.3s;
  245. }
  246. }
  247. &.flip {
  248. display: inline-flex;
  249. .c-image__flipper {
  250. position: relative;
  251. display: inline-flex;
  252. flex: 1 0 0;
  253. transition: transform 1s;
  254. transform-style: preserve-3d;
  255. &.toggle {
  256. transform: rotateY(180deg);
  257. }
  258. .front,
  259. .back {
  260. position: absolute;
  261. top: 0;
  262. left: 0;
  263. width: 100%;
  264. height: 100%;
  265. backface-visibility: hidden;
  266. }
  267. .back {
  268. transform: rotateY(180deg);
  269. }
  270. }
  271. }
  272. }
  273. </style>