js中background-position 无效

本文介绍了CSS中如何正确设置背景图片的位置,并给出了JavaScript中实现背景位置动态调整的正确方式。需要注意的是,在使用JS设置背景位置时,确保单位后添加空格。

css中正确书写background-position:x y; 坐标之间有空格
然而在写js的时候如果粗心会忘记第一个‘px’后面加个空格
正确js书写应该是backgroundposi:x+‘px ’+y+‘px’

.bf_tips { position:absolute; display:none; min-width:100px;max-width:212px; z-index:0; overflow:hidden; white-space:normal; word-wrap:break-word; } .bf_tips > .head{ height:7px; background:url(../images/tips/tips_bg.png) left top no-repeat;} .bf_tips > .head.left{width:45px;position:absolute;left:0px;} .bf_tips > .head.right{width:45px;background-position: right -7px;overflow:hidden;position:absolute;right:0px;} .bf_tips > .head.middle{background:url(../images/tips/bf_tips_m.png) 0 -7px;overflow:hidden;margin-left:45px; margin-right:45px;} .bf_tips > .mid, .bf_tips_container{background: url(../images/tips/tips_bg_lr.png) left top repeat-y;} .bf_tips > .mid{padding-left:13px;} .bf_tips_container {background-position:right top; padding-right:15px;} .bf_tips_content {background:#f6f6f6;} .bf_tips > .foot{height:15px; background:url(../images/tips/tips_bg.png) left -14px no-repeat;} .bf_tips > .foot.left {width:45px;position:absolute;left:0px;} .bf_tips > .foot.right{width:45px;background-position:right -29px;overflow:hidden;position:absolute;right:0px;} .bf_tips > .foot.middle{background:url(../images/tips/bf_tips_m.png) 0 -21px;overflow:hidden;margin-left:45px; margin-right:45px;} /*[direction: arrowtop]*/ .bf_tips.arrowtop > .head.left {height:13px; background-position:left -44px;} .bf_tips.arrowtop > .head.right{height:13px; background-position:right -57px;} .bf_tips.arrowtop > .head.middle{height: 13px; background-position:right -47px; background-repeat:repeat-x;} .bf_tips.arrowtop > .foot.left {height: 9px; background-position:left -70px;} .bf_tips.arrowtop > .foot.right{height: 9px; background-position:right -79px;} .bf_tips.arrowtop > .foot.middle {height: 9px; background-position:left -79px; background-repeat:repeat-x;} /*[tips width validation error]*/ .bf_tips_error { padding: 0px 0px 5px 20px; background: url(../images/svg/information.svg) no-repeat; color: #cc0000; }
最新发布
11-09
<div class="hour-row" v-for="(hour, hIndex) in hourValues" :key="hIndex"> <div class="time-cell time-cell-bg" v-for="(day, dIndex) in weekDays" :key="dIndex" :style="getCellStyle(dIndex, hIndex, itemBgHeight, itemPosition)" :class="{ selected: isSelected(dIndex, hIndex) }" :data-day="dIndex" :data-hour="hIndex" :data-minute="0"> {{ hIndex }} </div> </div> <button @click="getHeight">点击</button> getCellStyle(day, hour, itemBgHeight, itemPosition) { const cell = this.cellStates[`${day},${hour}`]; // return cell ? { backgroundColor: cell.color}; this.itemBgHeight = itemBgHeight; this.itemPosition = itemPosition; if (cell) { console.log('cell', cell); } return cell ? { '--bg-height': this.itemBgHeight, '--position': this.itemPosition } : {}; }, getHeight() { const day = 0; const startTimeH = 2; const itemBgHeight = 60; this.getCellStyle(day, startTimeH, `${itemBgHeight}%`, 'bottom'); }, //cell背景渐变色 .time-cell-bg { /* 基础样式 */ // width: 300px; // height: 200px; // border: 1px solid #ccc; /* 背景渐变实现 */ background-image: linear-gradient(var(--gradient-direction, to bottom), $blueColor, $blueColor var(--bg-height, 0%), transparent var(--bg-height, 0%), transparent); /* 根据位置调整渐变方向 */ background-position: var(--bg-position); background-repeat: no-repeat; } /* 动态调整位置 */ .time-cell-bg[style*="top"] { --gradient-direction: to bottom; --bg-position: top; } .time-cell-bg[style*="bottom"] { --gradient-direction: to top; --bg-position: bottom; } 上面方法点击按钮,改变数组里某个div的背景颜色高度,其他的背景颜色高度保持不变
08-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值