Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Javascript改变背景位置?_Javascript_Html_Css_Image_Css Sprites - Fatal编程技术网

用Javascript改变背景位置?

用Javascript改变背景位置?,javascript,html,css,image,css-sprites,Javascript,Html,Css,Image,Css Sprites,虽然在我真正需要一个不使用JQuery的解决方案之前,我已经看到有人问过这个问题,但这是一个嵌入式web界面,我不想要加载JQuery的开销。我需要能够在单个页面上使用JS操作精灵,精灵的状态取决于某些JS变量。我确信这一定是可能的,但是如果不使用JQuery,就找不到任何东西。我认为您可以使用Object.style.backgroundPosition=“position”来更改所需的背景位置 试试这个代码 <!DOCTYPE html> <html> <he

虽然在我真正需要一个不使用JQuery的解决方案之前,我已经看到有人问过这个问题,但这是一个嵌入式web界面,我不想要加载JQuery的开销。我需要能够在单个页面上使用JS操作精灵,精灵的状态取决于某些JS变量。我确信这一定是可能的,但是如果不使用JQuery,就找不到任何东西。

我认为您可以使用Object.style.backgroundPosition=“position”来更改所需的背景位置

试试这个代码

 <!DOCTYPE html>
<html>
<head>
<style>
div
{
background-image: url('example.png');
background-repeat: no-repeat;
width: 400px;
height: 400px;
border: 1px solid #000000;
}
</style>
<script>
function displayResult()
{
document.getElementById("div1").style.backgroundPosition="center bottom";  
}
</script>
</head>
<body>

<button type="button" onclick="displayResult()">Position background image</button>
<br>
<div id="div1">
</div>

</body>
</html>

div
{
背景图片:url('example.png');
背景重复:无重复;
宽度:400px;
高度:400px;
边框:1px实心#000000;
}
函数displayResult()
{
document.getElementById(“div1”).style.backgroundPosition=“center-bottom”;
}
定位背景图像


我认为您可以使用Object.style.backgroundPosition=“position”来更改所需的背景位置

试试这个代码

 <!DOCTYPE html>
<html>
<head>
<style>
div
{
background-image: url('example.png');
background-repeat: no-repeat;
width: 400px;
height: 400px;
border: 1px solid #000000;
}
</style>
<script>
function displayResult()
{
document.getElementById("div1").style.backgroundPosition="center bottom";  
}
</script>
</head>
<body>

<button type="button" onclick="displayResult()">Position background image</button>
<br>
<div id="div1">
</div>

</body>
</html>

div
{
背景图片:url('example.png');
背景重复:无重复;
宽度:400px;
高度:400px;
边框:1px实心#000000;
}
函数displayResult()
{
document.getElementById(“div1”).style.backgroundPosition=“center-bottom”;
}
定位背景图像

最简单的方法(我认为)是定义自己的css类并更改certan事件上的类。i、 e

<style type="text/css">
.bg1{
/* Some attributes set here */
background-position:cen‌​ter;
}
.bg2{
/* Some attributes set here */
background-position:left;
}

</style>
最简单的方法(我认为)是定义自己的css类,并在certan事件上更改这些类。i、 e

<style type="text/css">
.bg1{
/* Some attributes set here */
background-position:cen‌​ter;
}
.bg2{
/* Some attributes set here */
background-position:left;
}

</style>

到目前为止你试过什么?我将使用不同的类名并使用JavaScript更改这些类。请给我们一个html文档的示例。getElementById(“some_id”)。setAttribute(“style”,“background position:center;”;到目前为止你试过什么?我将使用不同的类名并使用JavaScript更改这些类。请给我们一个html文档的示例。getElementById(“some_id”)。setAttribute(“style”,“background position:center;”;谢谢,这基本上是我使用的解决方案,除了我使用了函数:document.getElementById(“some_id”)。className as.class似乎不起作用?谢谢,这基本上是我使用的解决方案,除了我使用了函数:document.getElementById(“some_id”).className作为.class似乎不起作用?Firefox 26不会对背景位置的变化做出反应。在CSS.style.backgroundPosition中定义时,未定义。MDN上没有记录。Firefox 26不会对背景位置的变化做出反应。在CSS.style.backgroundPosition中定义时,未定义。MDN上没有记录。