banner.vue 4.9 KB

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