vue div backgroundSize 不起作用的解决办法
2018-02-27 00:00
120 查看
在使用绑定样式的功能时,发现对div的backgroundSize属性设置不起作用,于是只能 使用一个比较笨拙的办法,解决这个问题
初始加载是生效的,但是之后当点击按钮改变图片地址后,无法填充背景图片
改变背景之后
第一个使用的是计算属性,绑定style,使用background-size和backgSize都无效
下面的是使用原生js设置样式
第一个
第二个
初始加载是生效的,但是之后当点击按钮改变图片地址后,无法填充背景图片
改变背景之后
第一个使用的是计算属性,绑定style,使用background-size和backgSize都无效
下面的是使用原生js设置样式
第一个
<template> <div> <div class="img" :style="style"></div> <button @click="prev">prev</button> <button @click="next">next</button> </div> </template> <script> export default { name: "swiper", data() { return { cur_index: 0, imgs: [ 'https://i1.mifile.cn/a4/xmad_15195327867488_jlLnp.jpg', 'https://i1.mifile.cn/a4/xmad_15185161540821_qPMoX.jpg', 'https://i1.mifile.cn/a4/xmad_15193829171444_CQnuo.jpg', 'https://i1.mifile.cn/a4/xmad_15192945916761_ormJz.jpg', ], } }, computed: { style() { return { background: `url('${this.imgs[this.cur_index]}') no-repeat`, 'backgroundSize': 'contain' } } }, methods: { prev() { this.cur_index = (this.cur_index + this.imgs.length - 1) % this.imgs.length }, next() { this.cur_index = (this.cur_index + 1) % this.imgs.length } } } </script> <style scoped> .img { width: 400px; height: 300px; border: 1px solid black; } </style>
第二个
<template> <div> <div class="img" :style="style" ref="swiper"></div> <button @click="prev">prev</button> <button @click="next">next</button> </div> </template> <script> export default { name: "swiper", data() { return { cur_index: 0, imgs: [ 'https://i1.mifile.cn/a4/xmad_15195327867488_jlLnp.jpg', 'https://i1.mifile.cn/a4/xmad_15185161540821_qPMoX.jpg', 'https://i1.mifile.cn/a4/xmad_15193829171444_CQnuo.jpg', 'https://i1.mifile.cn/a4/xmad_15192945916761_ormJz.jpg', ], } }, watch: { cur_index() { this.refresh() } }, methods: { refresh() { this.$refs.swiper.style.background = `url('${this.imgs[this.cur_index]}') no-repeat` this.$refs.swiper.style.backgroundSize = 'contain' }, prev() { this.cur_index = (this.cur_index + this.imgs.length - 1) % this.imgs.length }, next() { this.cur_index = (this.cur_index + 1) % this.imgs.length } }, mounted() { this.refresh() } } </script> <style scoped> .img { width: 400px; height: 300px; border: 1px solid black; /*background-size: contain;*/ } </style>
相关文章推荐
- background-size:cover背景重复解决办法
- div vertical-align不起作用解决办法
- 移动设备部分不支持background—size解决办法
- 当外部div没有设置高度 或者内部div float之后 边框 内外边距等不起作用的解决办法
- div显示图片不全和background-size在ie和火狐下不生效的解决方法
- div vertical-align不起作用解决办法
- flex3 中 Legend fontSize不起作用的解决办法
- JQuery在UpdatePanel中当事件点击后JQuery事件不起作用的解决办法(JQuery与UpdatePanel问题解析)
- DIV内英文或者数字不换行的问题 解决办法
- 解决ie下部支持background-size:100%;属性的方法
- sga_max_size设置过大导致ORALCE无法启动ORA-27100解决办法
- 页面width与height使用百分比来划分不起作用解决办法--记录六
- 用Bitmap加载图片资源时,报错java.lang.OutOfMemoryError: bitmap size exceeds VM budget的解决办法
- IDocHostUIHandler::TranslateAccelerator快捷键不响应解决办法(即ctrl+c, ctrl+v不起作用解决办法)
- IE6下div图片下边4px空隙bug的解决办法
- 谷歌chrome浏览器下font-size属性值小于12px时无效问题的解决办法
- div或li float之后父容器高度不能自动增加的解决办法
- spring @Transactional 方法内事务不起作用的解决办法
- font-size=0的妙用,解决div塌陷
- Ajax 得到的值对全局变量赋值没有起作用解决办法和 Jquery append()进去的动态代码 点击的js没起作用