Carousel.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template lang="pug">
  2. .carousel-wrapper#carousel
  3. b-carousel(
  4. v-model="curIndex"
  5. :arrow="false"
  6. :pause-hover="false"
  7. :indicator="list.length > 1"
  8. indicator-style="is-lines"
  9. @change="onChange"
  10. )
  11. b-carousel-item(
  12. v-for="(item, idx) in list"
  13. :key="idx"
  14. @click="onClick(item)"
  15. )
  16. .pic(:style="{backgroundImage: `url('${item.image_url}')`, backgroundColor: item.theme_color || 'transparent'}")
  17. .container.is-content(v-if="item.title || item.desc" :style="{textAlign: item.cont_position || 'left'}")
  18. .text-content
  19. .title {{item.title}}
  20. .desc {{item.desc}}
  21. .button.is-primary.is-inverted.is-outlined(
  22. v-if="item.button_text"
  23. @click.stop="onClick(item, true)"
  24. ) {{item.button_text}}
  25. </template>
  26. <script lang="ts">
  27. import Vue from 'vue'
  28. const insertOrUpdateStyleTag = (item) => {
  29. const id = `carousel-style`
  30. const css = `.kaifain-view .carousel {background: ${item && item.theme_color || '#eee'}} .kaifain-view .carousel .carousel-item .text-content .button.is-primary:hover{color:${item && item.theme_color || '#20242d'}}`
  31. let dom = document.getElementById(id)
  32. if (!dom) {
  33. const wrapper = document.querySelector('#carousel')
  34. if (wrapper) {
  35. dom = document.createElement('style')
  36. dom.id = id
  37. dom.innerHTML = css
  38. wrapper.appendChild(dom)
  39. }
  40. } else {
  41. dom.innerHTML = css
  42. }
  43. }
  44. export default Vue.extend({
  45. name: 'Carousel',
  46. components: {
  47. },
  48. props: {
  49. list: Array
  50. },
  51. data() {
  52. return {
  53. curIndex: 1
  54. }
  55. },
  56. methods: {
  57. onClick(item, isButton = false) {
  58. if (!isButton && item.button_text) {
  59. return
  60. }
  61. window.open(item.jump_target)
  62. },
  63. onChange(idx: number) {
  64. const item = this.list[idx]
  65. item && insertOrUpdateStyleTag(item)
  66. }
  67. }
  68. })
  69. </script>
  70. <style lang="scss">
  71. .kaifain-view .carousel {
  72. width: 100%;
  73. height: 400px;
  74. background: #eee;
  75. .carousel-indicator {
  76. margin-bottom: 16px;
  77. .indicator-item {
  78. .indicator-style.is-lines {
  79. width: 48px;
  80. height: 4px;
  81. opacity: 0.2;
  82. }
  83. &.is-active .indicator-style {
  84. opacity: 0.8;
  85. }
  86. }
  87. }
  88. .carousel-item {
  89. width: 100%;
  90. height: 400px;
  91. .pic {
  92. background-position: center center;
  93. background-repeat: no-repeat;
  94. background-size: auto 100%;
  95. position: absolute;
  96. top: 0;
  97. left: 0;
  98. width: 100%;
  99. height: 100%;
  100. }
  101. .text-content {
  102. position: relative;
  103. z-index: 1;
  104. margin-top: 140px;
  105. > * {
  106. color: #fff;
  107. }
  108. .desc {
  109. opacity: 0.76;
  110. }
  111. .button {
  112. margin-top: 44px;
  113. min-width: 128px;
  114. height: 2.572em;
  115. &.is-primary {
  116. font-size: 0.875rem;
  117. &:hover {
  118. color: #2487ff;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. </style>