banner.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <div class="banner " v-ratio="(baseHeight/baseWidth)">
  3. <!-- 轮播图片,通过show切换-->
  4. <div
  5. v-for="(item,i) in banners"
  6. :key="'img-'+item.id"
  7. :class="{
  8. 'banner-show':true,
  9. 'banner-show-now':i===bannerIndex
  10. }"
  11. v-show="i===bannerIndex"
  12. >
  13. <!-- 图片框-->
  14. <img :src="item.img" :alt="item.title">
  15. <!-- 文本框-->
  16. <div class="w-p1200 h-full py-2 border-box relative" v-show="showText">
  17. <div class="view-text">
  18. <div class="title">{{item.title}}</div>
  19. <div class="content indent-8">
  20. {{item.text}}
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. <!-- 点击切换控制 -->
  26. <div class="banner-control bottom-14">
  27. <div
  28. v-for="(item,i) in banners"
  29. :key="'handle-'+item.id"
  30. :class="{
  31. 'banner-handle':true,
  32. 'banner-handle-now':i===bannerIndex
  33. }"
  34. @click="changeBanner(i)"
  35. >
  36. </div>
  37. </div>
  38. </div>
  39. </template>
  40. <script>
  41. export default {
  42. name: "banner",
  43. props: {
  44. waitTime: {default:5000}
  45. },
  46. data(){
  47. return {
  48. banners:[{
  49. id: '1',
  50. url:'#/recommends',
  51. title: 'text',
  52. text: '1234',
  53. img:'image/banner/index.jpg',
  54. diffColor: "#000"
  55. },{
  56. id: '2',
  57. url:'#/recommend?recommendId=2',
  58. title: '',
  59. text: '',
  60. img:'image/banner/banner_show.png',
  61. diffColor: "#fafafa"
  62. },{
  63. id: '3',
  64. url:'http://',
  65. title: '',
  66. text: '',
  67. img:'image/banner/banner_gb28181.jpg',
  68. diffColor: "#ffffff"
  69. },
  70. ],
  71. bannerIndex:0,
  72. timer:null,
  73. showText: false,
  74. baseWidth: 1903,
  75. baseHeight: 630,
  76. // 比例
  77. }
  78. },mounted() {
  79. this.cursor()
  80. },
  81. directives: {
  82. // 为元素设定比例
  83. ratio: {
  84. bind: function (el, binding) {
  85. // 如果没有传入比例值,则默认为1
  86. let ratio = binding.value || 1;
  87. let width = el.offsetWidth;
  88. let finalHeight = width * ratio;
  89. // 防止高度过低导致影响美观
  90. if (width < 1000) {
  91. finalHeight+= 50;
  92. }
  93. el.style.height = finalHeight + 'px'
  94. }
  95. }
  96. },
  97. methods:{
  98. cursor(){
  99. clearTimeout(this.timer);
  100. this.timer=setTimeout(()=>{
  101. this.nextBanner();
  102. },this.waitTime);
  103. },
  104. nextBanner(){
  105. let nextBanner = this.bannerIndex+1;
  106. if(nextBanner>=this.banners.length){
  107. nextBanner = 0;
  108. }
  109. this.changeBanner(nextBanner);
  110. },
  111. changeBanner(i){
  112. this.bannerIndex = i;
  113. this.cursor();
  114. }
  115. }
  116. }
  117. </script>
  118. <style scoped>
  119. .banner {
  120. width: 100%;
  121. position: relative;
  122. display: flex;
  123. overflow: hidden;
  124. height: 615px;
  125. }
  126. .banner .banner-show {
  127. position: absolute;
  128. width: 100%;
  129. height: 100%;
  130. left: 0;
  131. top: 25px;
  132. display: flex;
  133. justify-content: center;
  134. }
  135. .banner .banner-show .view-text {
  136. height: 80%;
  137. width: 480px;
  138. border-radius: 3px;
  139. background-color: rgba(0, 0, 0, 0.47);
  140. color: floralwhite;
  141. padding: 2px 5px;
  142. }
  143. .banner .banner-show .view-text .title {
  144. font-size: 1.7em;
  145. }
  146. .banner .banner-show .view-text .content {
  147. font-size: 0.9em;
  148. }
  149. .banner .banner-show img {
  150. width: 100%;
  151. height: auto;
  152. position: absolute;
  153. left: 0;
  154. top: 0;
  155. z-index: 2;
  156. }
  157. .banner .banner-show .blurImg{
  158. top: -110px;
  159. }
  160. .banner .banner-control {
  161. width: auto;
  162. height: auto;
  163. position: absolute;
  164. left: 50%;
  165. bottom: 15px;
  166. transform: translate(-50%, 0);
  167. z-index: 10;
  168. display: flex;
  169. padding: 5px;
  170. border-radius: 3px;
  171. background-color: #fff;
  172. opacity: 0.8;
  173. }
  174. .banner .banner-control .banner-handle {
  175. width: 10px;
  176. height: 5px;
  177. border-radius: 2px;
  178. transition: all 0.6s;
  179. margin: 0 5px;
  180. cursor: pointer;
  181. background-color: rgba(178, 160, 160, 0.79);
  182. }
  183. .banner .banner-control .banner-handlehover {
  184. background-color: rgba(196, 167, 167, 0.79);
  185. }
  186. .banner .banner-control .banner-handle-now {
  187. width: 15px;
  188. background-color: #f49b00;
  189. }
  190. </style>