CImage.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. export default {
  62. name: 'CImage',
  63. inject: ['control'],
  64. props: {
  65. node: {
  66. type: Object,
  67. default: null
  68. }
  69. },
  70. data () {
  71. return {
  72. img: null,
  73. frontImage: null,
  74. backImage: null,
  75. toggle: false
  76. }
  77. },
  78. computed: {
  79. isSnapping () {
  80. return this.control.snapping
  81. },
  82. styles () {
  83. const {
  84. width,
  85. height,
  86. radius
  87. } = this.node
  88. return {
  89. width: `${width}px`,
  90. height: `${height}px`,
  91. 'font-size': `${Math.min(width, height) / 3 | 0}px`,
  92. 'border-radius': `${radius}px`,
  93. 'background-image': !this.isEmpty && this.isNormal ? `url(${getThumbnailUrl(this.img)})` : 'none'
  94. }
  95. },
  96. className () {
  97. return this.isSnapping ? {
  98. empty: this.isEmpty
  99. } : {
  100. empty: this.isEmpty,
  101. slide: this.isSlide,
  102. fade: this.isFade,
  103. flip: this.isFlip || this.isSectionFlip
  104. }
  105. },
  106. isEmpty () {
  107. return !this.img
  108. },
  109. slideStyles () {
  110. return {
  111. 'background-image': this.img ? `url(${getThumbnailUrl(this.img)})` : 'none'
  112. }
  113. },
  114. flipFrontStyles () {
  115. return {
  116. 'background-image': this.frontImage ? `url(${getThumbnailUrl(this.frontImage)})` : 'none'
  117. }
  118. },
  119. flipBackStyles () {
  120. return {
  121. 'background-image': this.backImage ? `url(${getThumbnailUrl(this.backImage)})` : 'none'
  122. }
  123. },
  124. isNormal () {
  125. const { animation } = this.node
  126. return this.isSnapping || animation === 'none' || animation === 'fade'
  127. },
  128. isFade () {
  129. return this.node.animation === 'fade'
  130. },
  131. isSlide () {
  132. return this.node.animation === 'slide'
  133. },
  134. isFlip () {
  135. return this.node.animation === 'flip'
  136. },
  137. isSectionFlip () {
  138. return this.node.animation === 'section-flip'
  139. },
  140. sctionWidth () {
  141. return this.width / 10
  142. },
  143. sectionStyles () {
  144. const { width } = this.node
  145. const offset = width / 10
  146. const styles = []
  147. for (let i = 0; i < 10; i++) {
  148. styles.push({
  149. 'background-size': `${width}px 100%`,
  150. 'background-position-x': `-${offset * i}px`
  151. })
  152. }
  153. return styles
  154. }
  155. },
  156. watch: {
  157. 'node.sources' () {
  158. this.reset()
  159. },
  160. 'node.interval' (val) {
  161. if (this.node.sources.length > 1) {
  162. clearTimeout(this.$timer)
  163. this.$timer = setTimeout(this.toggleImage, val * 1000)
  164. }
  165. },
  166. isSnapping (val) {
  167. if (this.node.sources.length > 1) {
  168. if (val) {
  169. clearTimeout(this.$timer)
  170. } else {
  171. this.$timer = setTimeout(this.toggleImage, val * 1000)
  172. }
  173. }
  174. }
  175. },
  176. created () {
  177. this.$timer = -1
  178. this.reset()
  179. },
  180. beforeDestroy () {
  181. clearTimeout(this.$timer)
  182. },
  183. methods: {
  184. reset () {
  185. this.$index = 0
  186. this.choose()
  187. this.changeFlip()
  188. },
  189. changeFlip () {
  190. if (this.toggle) {
  191. this.backImage = this.img
  192. } else {
  193. this.frontImage = this.img
  194. }
  195. },
  196. toggleImage () {
  197. this.toggle = this.choose()
  198. this.changeFlip()
  199. },
  200. choose () {
  201. const { sources, interval, toggleType } = this.node
  202. const length = sources.length
  203. clearTimeout(this.$timer)
  204. if (length === 0) {
  205. this.img = null
  206. return this.toggle
  207. }
  208. if (length === 1) {
  209. this.img = sources[0]
  210. return this.toggle
  211. }
  212. switch (toggleType) {
  213. case 'cycle':
  214. this.$index = (this.$index + 1) % length
  215. break
  216. case 'random':
  217. this.$index = Math.random() * length | 0
  218. if (sources[this.$index]?.keyName === this.img.keyName) {
  219. if (this.$index === 0) {
  220. this.$index += 1
  221. } else {
  222. this.$index -= 1
  223. }
  224. }
  225. break
  226. default:
  227. return
  228. }
  229. this.img = sources[this.$index]
  230. this.$timer = setTimeout(this.toggleImage, interval * 1000)
  231. return !this.toggle
  232. }
  233. }
  234. }
  235. </script>
  236. <style lang="scss" scoped>
  237. .c-image {
  238. position: relative;
  239. display: inline-block;
  240. overflow: hidden;
  241. &.empty {
  242. display: inline-flex;
  243. justify-content: center;
  244. align-items: center;
  245. background-color: rgba(255, 255, 255, 0.8);
  246. &::before {
  247. content: "\e612";
  248. font-size: inherit;
  249. }
  250. }
  251. &.fade {
  252. transition: background-image 1s;
  253. }
  254. &.slide {
  255. .slide-enter {
  256. transform: translateX(100%);
  257. }
  258. .slide-leave-to {
  259. transform: translateX(-100%);
  260. }
  261. .c-image__slider {
  262. position: absolute;
  263. width: 100%;
  264. height: 100%;
  265. transition: transform 0.3s;
  266. }
  267. }
  268. &.flip {
  269. display: inline-flex;
  270. .c-image__flipper {
  271. position: relative;
  272. display: inline-flex;
  273. flex: 1 0 0;
  274. transition: transform 1s;
  275. transform-style: preserve-3d;
  276. &.toggle {
  277. transform: rotateY(180deg);
  278. }
  279. .front,
  280. .back {
  281. position: absolute;
  282. top: 0;
  283. left: 0;
  284. width: 100%;
  285. height: 100%;
  286. backface-visibility: hidden;
  287. }
  288. .back {
  289. transform: rotateY(180deg);
  290. }
  291. }
  292. }
  293. }
  294. </style>