[英]Detecting background image top margin with javascript
This is how I detect the top margin of a div and increase/decrease it: 这就是我检测div的上边距并增加/减少它的方式:
var oldm = $("#bdi").css("margin-top").replace("px", "");
var addm = 1;
$("#bdi").css({
'margin-top': '-='+addm+'px'
})
But I need to do the same with background position. 但是我需要对背景位置做同样的事情。
For example: 例如:
background-position: center 5px;
How do I detect "5px" and increase/decrease it? 如何检测“ 5px”并增加/减少它?
Thanks 谢谢
You can get it using background-position-y like this : 您可以像这样使用background-position-y来获取它:
var bgPositionY = ($("#bdi").css('background-position-y'))
var addPos = 5;
$("#bdi").css({
'background-position-y': '-='+addPos+'px'
})
https://jsfiddle.net/IA7medd/aLkok1n4/ https://jsfiddle.net/IA7medd/aLkok1n4/
You don't need jQuery for this... 您不需要jQuery ...
var el = document.getElementById('bdi'),
currentYPosition = getComputedStyle(el)['backgroundPositionY'],
increment = 1,
newYPosition = 'calc(' + currentYPosition + ' + ' + increment + 'px)';
// Set new backgroundPositionY
el.style.backgroundPositionY = newYPosition;
The use of calc()
above ensures the value is properly incremented, even if a percentage position value is used. 即使使用百分比位置值,上面的
calc()
的使用也可以确保值正确增加。
如果您使用的是jQuery,最简单的选择是使用内联增量表示法:
$("#bdi").css('background-position-y', '+=5px');
As of jQuery 1.6, .css() accepts relative values similar to .animate().
从jQuery 1.6开始,.css()接受类似于.animate()的相对值。 Relative values are a string starting with += or -= to increment or decrement the current value.
相对值是一个以+ =或-=开头的字符串,用于递增或递减当前值。 Link
链接
For example like this 像这样
$("div").css("background-position-x", "+=10px");
$("div").css("background-position-Y", "-=10px");
If you want to do other operation on css value use this 如果要对css值执行其他操作,请使用此
$("div").css("background-position-x", function(index) {
return index * 10;
});
You can see demo at bottom 您可以在底部看到演示
$("#increaseX").click(function(){ $("#image").css("background-position-x", "+=10px"); }); $("#decreaseX").click(function(){ $("#image").css("background-position-x", "-=10px"); }); $("#increaseY").click(function(){ $("#image").css("background-position-y", "+=10px"); }); $("#decreaseY").click(function(){ $("#image").css("background-position-y", "-=10px"); });
#image { width: 100px; height: 100px; background-image: url("https://assets.servedby-buysellads.com/p/manage/asset/id/28536"); background-position: 0px 0px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="image"></div> <button id="increaseX">increaseX</button> <button id="decreaseX">decreaseX</button> <br /> <button id="increaseY">increaseY</button> <button id="decreaseY">decreaseY</button>
Firefox doesn't support background-position-x
and background-position-y
. Firefox不支持
background-position-x
和background-position-y
。 If you want to do your target work in firefox, see jsfiddle 如果要在firefox中完成目标工作,请参阅jsfiddle
You can 您可以
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.