123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <style>
- .swiper-img .bg{ width: 100%;}
- .slide-indicator{position:absolute;bottom:90rpx;left:0px;width:100%}
- .slide-indicator-item{width:20rpx;height:2rpx;background:rgba(255,255,255,0.4);margin:0 10rpx}
- .slide-indicator-item.select{background:white}
- </style>
- <template>
- <view>
- <view id="swiper" class="swiper">
- <swiper class="swiper-img" :style="height > 0 ? ('height:' + height + 'px') : '' " :indicator-dots="indicator" autoplay="true" @change="tapChange">
- <swiper-item @tap="tapItem(index)" v-for="(item,index) in swData">
- <image mode="widthFix" class="bg" :src="item.img"/>
- </swiper-item>
- </swiper>
- </view>
- </view>
-
- </template>
- <script>
- var swH = 0;
- export default {
- name: 'ui-swiper',
- props:{
- swData : {
- type: Array,
- default: []
- },
- indicator:{
- type:Boolean,
- default : false
- }
-
- },
- data() {
- return{
- imgsAr : [],
- index : 0,
- width : 0,
- height:0
- }
- },
- watch: {
- swData() {
- this.initView();
- }
- },
- created() {
- this.initView();
- this.$nextTick(() => {
- const query = uni.createSelectorQuery().in(this);
- query.select("#swiper").boundingClientRect(res=>{
- console.log("xxxxx");
- this.width = res.width;
- if(this.swData.length > 0) this.initImg(this.index);
- }).exec();
- })
-
-
- },
- mounted() {
- //this.width = ;
- },
- methods: {
-
- tapItem:function(index){
- this.$emit('tapItem',index);
- },
- tapChange:function(ev){
- this.index = ev.detail.current;
- if(this.imgsAr[this.index] != null) {
- this.setHeight(this.imgsAr[this.index].height);
- }
- },
- /**
- * 初始化
- */
- initView:function(){
- this.imgsAr = [];
-
- for(var i in this.swData) {
- this.imgsAr[i] = null;
- this.initImg(i);
- }
- if(swH > 0) {
- this.height = swH;
- }
- },
- /**
- * 初始化
- * @param {Object} i
- */
- initImg:function(i) {
-
- uni.getImageInfo({
- src:this.swData[i].img,
- success:(img) => {
- var width = 0;
- var height = 0;
- if(img.width > this.width) {
- width = this.width;
- } else {
- width = img.width;
- }
-
- //"width": 1500,
- //"height": 972,
- height = (img.height / img.width) * width;
- this.imgsAr[i] = { width:width, height:height, img:this.swData[i]};
- console.log(this.imgsAr[i]);
- if(this.index == i) {
- this.setHeight(height);
- }
- }
- });
- },
- setHeight:function (height)
- {
- this.height = height;
- swH = height;
- this.$emit('onHeight',{height:height,index:this.index});
- }
- }
- }
- </script>
|