場景:點擊圖片 進行預覽,跳出一個modal框,可進行放大縮小的操作。
關鍵:樣式 transform
- CSS
transform
屬性允許你旋轉,縮放,傾斜或平移給定元素。這是通過修改CSS視覺格式化模型的坐標空間來實現的。 - 給元素動態綁定transform的屬性值,通過點擊事件去修改
1 <!-- 預覽圖片 --> 2 <template> 3 <a-modal :visible="visible" :footer="null" @cancel="handleCancel" wrapClassName="box"> 4 <div class="head-btn-box"> 5 <a-button icon="plus" @click="toBIgChange">放大</a-button> 6 <a-button icon="minus" @click="toSmallChange">縮小</a-button> 7 </div> 8 <img 9 :src="url" 10 alt 11 class="img" 12 :style="{transform:'scale('+multiples+')'}" 13 /> 14 </a-modal> 15 </template> 16 17 <script> 18 export default { 19 data() { 20 return { 21 visible: false, 22 url: "", 23 multiples: 1, 24 }; 25 }, 26 27 props: {}, 28 29 components: {}, 30 31 created() {}, 32 33 computed: {}, 34 35 mounted() {}, 36 37 methods: { 38 show(url) { 39 this.visible = true; 40 this.url = url; 41 }, 42 43 handleCancel() { 44 this.visible = false; 45 this.multiples = 1 46 }, 47 48 toBIgChange() { 49 if (this.multiples >= 2) { 50 return; 51 } 52 this.multiples += 0.25; 53 }, 54 // 縮小 55 toSmallChange() { 56 if (this.multiples <= 1) { 57 return; 58 } 59 this.multiples -= 0.25; 60 }, 61 }, 62 }; 63 </script> 64 <style scoprd> 65 .head-btn-box { 66 margin-bottom: 20px; 67 } 68 .img { 69 width: 200px; 70 } 71 .box .ant-modal-body { 72 width: 500px; 73 overflow-x: auto; 74 height: 500px; 75 overflow-y: auto; 76 } 77 </style>
效果:
第一篇隨筆,寫的比較簡單。