Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.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 - Fatal编程技术网

如何使用javascript实现这一点?

如何使用javascript实现这一点?,javascript,html,css,Javascript,Html,Css,我昨天才开始学习javascript,因为我真的需要一个代码来完成以下示例:(我只计算了逻辑) 两个要素: <div class="item bw" style="background-position: -10px -330px"> <div class="item bw" style="background-position: -10px -390px"> 对不起。我只是想弄清楚我需要什么 我用这个代码来工作: window.onload = function()

我昨天才开始学习javascript,因为我真的需要一个代码来完成以下示例:(我只计算了逻辑)

两个要素:

<div class="item bw" style="background-position: -10px -330px">
<div class="item bw" style="background-position: -10px -390px">
对不起。我只是想弄清楚我需要什么


我用这个代码来工作:

window.onload = function() 
{
        var bws = document.querySelectorAll(".bw");
        for (var i = bws.length-1; i >= 0; i-- )
        {
            var ypos, pos;
            pos = bws[i].style.backgroundPosition.split(' ');
            ypos = pos[1].match(/(-\d+)px$/);
            bws[i].style.backgroundPosition = pos[0] + " " + (parseInt(ypos[1])-370)+"px";
        }
};
它基于Niet黑暗绝对答案

您可以使用jquery

$('.item.bw').css('background-position', newValue);
或者只有Y值

$('.item.bw').css('background-position-y', newValue);

为什么可以,特别是因为初始值是在
style
属性中定义的,这比在CSS文件中容易得多

var bws = document.querySelectorAll(".bw"),
    l = bws.length, i, pos, match;
for( i=0; i<l; i++) {
    pos = bws[i].style.backgroundPosition || bws[i].style.backgroundPositionY;
    match = pos.match(/(-\d+)px$/);
    if( match) {
        bws[i].style.backgroundPositionY = (match[1]-300)+"px";
    }
}
var bws=document.queryselectoral(“.bw”),
l=bws.长度,i,位置,匹配;
对于(i=0;i
Ok),对于这一点,我需要HTML页面的整个dom结构。但我会尝试逻辑地回答。(希望我正确理解了您的问题)
1) 假设我们编写了一个函数startOnLoad(){}
2) 使用event onLoad=“startLoading()”在body标记中调用此函数
3) 给你标签和身份证
4) 使用var Parent=document.getElementById()
5) 这取决于HTML dom解析的结构。
返回节点
子节点
长子
最后一个孩子
下一步
以前的兄弟姐妹
与Parent.nextSibling类似,将为下一个节点提供。
例如:temp=Parent.nextSibling;
temp.id将返回节点的id。存储“btw”的id值和do字符串比较
6) 如果为true,则使用以下命令
要获取或设置的Object.style.backgroundPosition。

你看到jQuery标记了吗?为什么是-2,这是更改位置的代码是的,在jQuery中,这不包括在问题中。jQuery是JAVASCRIPT还是完全不同的语言Niels?尽管jQuery不是浏览器的本机语言。太神奇了,它是标准的。你希望这些事情什么时候发生?当bw类被设置在元素上时,你想改变bg位置吗?你想做X,你认为你可以通过做Y来解决它,所以你问Y,这是一件奇怪的事情。问问X吧!你到底想干什么?最终结果应该是什么?@Tibos我想我很清楚当页面加载时会发生这种情况。
var bws = document.querySelectorAll(".bw"),
    l = bws.length, i, pos, match;
for( i=0; i<l; i++) {
    pos = bws[i].style.backgroundPosition || bws[i].style.backgroundPositionY;
    match = pos.match(/(-\d+)px$/);
    if( match) {
        bws[i].style.backgroundPositionY = (match[1]-300)+"px";
    }
}
Ok, for this I will need the entire dom structure of your HTML page. But I will try to answer logically. (Hoping that I understood your question correctly)

1) Lets say we write a function startOnLoad(){}
2) Call this function in the body tag with event onLoad = "startLoading()"
3) Give and id to you <body> tag
4) Use var Parent = document.getElementById(<bodytagId>)
5) After this depending on the structure of the HTML dom parsing.

parentNode
childNodes
firstChild
lastChild
nextSibling
previousSibling

like Parent.nextSibling will give the next node.
eg: temp = Parent.nextSibling;
    temp.id will return the id of the node. Store the value of id and do string comparison of 'btw'
6) if true, node use the following
Object.style.backgroundPosition to get or to set.