jquery background change one axis only

谁说胖子不能爱 提交于 2019-12-07 03:27:35

问题


I think my last question overwhelmed everybody so I will simplify

I am attempting to change the background-position of the x-axis only. By default if only one value is defined, the other defaults to 50%, so this function:

function colorChangePiano() {
    var bp = $("background-position").css;
    $("#target").css('background-position', (bp == -1131) ? '-377' : '0');
}

returns background-position: 0 50%;

How can I modify the function to change x-axis but leave y-axis unchanged?

edited --> this is for a sprite with 4 columns and 4 rows so the y-axis should remain dynamic. working site here: http://www.avalonbyeaw.com click "finishes." we got it working with some crazy complicated scripting on the live site, but i will leave this up here anyway because i would still really like to find a solution to this problem


回答1:


Here's how I would do it.

$('#target').css('background-position', function(i, backgroundPosition) {
    return backgroundPosition.replace(/\d+px/, '60px');
});



回答2:


Add the 2 values to background-position (as it expects). Also save the current position in a variable which makes this easier.

var pos = -377;
var colorChangePiano = function() {
  if (pos == -377) {
    pos = -1131
  }
  else {
    pos = -377
  }
  $('#target').css({
    'backround-position': pos + 'px 0px' // will result in "Xpx 0px"
  });
}


来源:https://stackoverflow.com/questions/11006709/jquery-background-change-one-axis-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!