vue 获取当前屏幕的宽度,图片等比例缩放,动态设置css样式
vue 获取当前屏幕的宽度,图片等比例缩放
这个是一个背景图,点击对应的圆圈就会出现一个蒙层上面显示详情,但是只要当前窗口大小变化了,因为我的蒙层大小是固定的px,所以不会变,就不在图片对应的位置了
我们要绑定样式style,等后面要根据当前的屏幕来给蒙层动态设置宽高
<div class="main"> <div class="content"> <div class="street"> <router-link :to="{name:‘/streetDetails‘,query:{code:‘310151101‘}}" :style="style01" class="street-item item1" ><span>详情</span></router-link > </div> </div> </div>
css主要代码
.main { background: transparent url("../../src/assets/img/street.png") no-repeat; padding-top: 50.78%; position: relative; background-size: 100%; & .content { border: 0px; } } .street-item { height: 92px; width: 92px; position: absolute; max-width: 92px; min-width: 70px; min-height: 92px; min-height: 70px; text-align: center; display: block; line-height: 92px; // background: #c1bcbc82; border-radius: 50%; // border: 1px solid red; & span { color: white; display: none; } } a:hover { & span { display: inline-block; } background: #4b42426b; } .item1 { position: absolute; top: 11.3%; left: 18.1%; }
主要js代码*
data() { return { style01: { width: 92, height: 92, lineheight: 92, borderRadius: 50 }, centerWidth: "", centerHeight: "" }; }, mounted() { //立即执行 window.onresize = () => { return (() => { this.resizeWin(); })(); }; this.resizeWin(); }, methods: { resizeWin() { console.log(document.documentElement.clientWidth); //当前window 宽 this.centerWidth = document.documentElement.scrollWidth; //最小宽度 1440 var boxH = 92, boxW = 92; if (this.centerWidth > 1440) { boxH = boxW = Math.ceil((this.centerWidth / 1920) * 92); // console.log("H", boxH); } else { boxH = boxW = Math.ceil((1440 / 1920) * 92); } // var item = document.getElementsByClassName("street-item"); console.log(this.style01.width); this.style01.width = boxW + "px"; this.style01.height = boxH + "px"; this.style01.lineHeight = boxH + "px"; this.style01.borderRadius = boxW / 2 + "px"; } }
相关推荐
background-color: blue;background-color: yellow;<input type="button" value="变蓝" @click="changeColorT
昔人已老 2020-07-19
yuzhu 2020-11-16
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
sjcheck 2020-11-03
怪我瞎 2020-10-28
源码zanqunet 2020-10-28
gloria0 2020-10-26
王军强 2020-10-21
学习web前端 2020-09-28
QiaoranC 2020-09-25
anchongnanzi 2020-09-21
安卓猴 2020-09-12
Macuroon 2020-09-11
kiven 2020-09-11
LittleCoder 2020-09-11
Cheetahcubs 2020-09-13
小焊猪web前端 2020-09-10
颤抖吧腿子 2020-09-04