繁体   English   中英

如何移动由CSS样式表设置样式的元素?

[英]How to move an element styled by CSS stylesheet?

我有要移动的图像。 如果我具有以下HTML,则可以使用javascript移动元素:

<html>
<head>
    <title>Snake</title>
    <script type="text/javascript" src="snake.js"></script>
    <!-- <link type="text/css" href="snake.css"> -->
</head>
<body onKeyPress="ProcessKeypress(event);">
    <p><img id="snake" style="z-index: 0; left: 300px; position: absolute; top: 250px" 
    float=top border=0 hspace=0 src="snake.gif"></p>
</body>

但是我想使用CSS监视image元素。 当我这样做时,我的代码无法移动图像。 下面是我想使用的HTML和CSS。

<html>
<head>
    <title>Snake</title>
    <script type="text/javascript" src="snake.js"></script>
    <link type="text/css" href="snake.css">
</head>
<body onKeyPress="ProcessKeypress(event);">
    <p><img id="snake" src="snake.gif"></p>
</body>

@CHARSET "UTF-8";
img {
z-index: 0;
left: 300px; 
position: absolute; 
top: 250px;
float: top;
border: 0;
hspace: 0;
}

下面的JavaScript是我用来移动图像的东西。 任何和所有帮助表示赞赏。

function moveObj(name, Xpix, Ypix) 
{    
    obj = document.getElementById(name);

    px = parseInt(obj.style.left) + Xpix;       
    py = parseInt(obj.style.top) + Ypix;
    obj.style.left = px;
    obj.style.top = py;
}

function ProcessKeypress(e)
{
    var myObj = 'snake';
    var moveBy = 10;

    if(e.keyCode === 97) {
        moveObj(myObj, -moveBy, 0);
    }
    else if(e.keyCode === 100) {
        moveObj(myObj, moveBy, 0);
    }
    else if(e.keyCode === 115) {
        moveObj(myObj, 0, moveBy);
    }
    else if(e.keyCode === 119) {
        moveObj(myObj, 0, -moveBy);
    }
}

我不确定您的代码是否为复制粘贴,但是您需要关闭链接标记才能链接到CSS。

<link rel="stylesheet" type="text/css" href="location" />

但这是没有意义的。 这是转贴,因此我将其标记为。 请参阅为什么element.style.left返回NaN?

那应该回答你的问题。 当我处理您的代码时,它回答了我的问题,这很有意义。 祝好运。

obj.style.left访问内联定义的样式属性。

更新

这是您的纯JavaScript解决方案

函数:getStyleProp()获取当前应用的样式[源]

function getStyleProp(elem, prop){
    if(window.getComputedStyle)
        return window.getComputedStyle(elem, null).getPropertyValue(prop);
    else if(elem.currentStyle) return elem.currentStyle[prop]; //IE
}

函数:moveObj()

function moveObj(obj, Xpix, Ypix) {  
    obj.style.left = parseInt(getStyleProp(obj,'left')) + Xpix+"px";
    obj.style.top= parseInt(getStyleProp(obj,'top')) + Ypix + "px";
}

函数:ProcessKeypress()

function ProcessKeypress(e) {
    var myObj = document.getElementById("snake");
    var moveBy = 10;
    switch(e.charCode) {
        case 97: moveObj(myObj, -moveBy, 0); break;
        case 100: moveObj(myObj, moveBy, 0); break;
        case 115: moveObj(myObj, 0, moveBy); break;
        case 119: moveObj(myObj, 0, -moveBy); break;            
    }
}    

我还使用jQuery解决了您的案例: 演示

函数moveObj

function moveObj(obj, Xpix, Ypix) {    
    obj.css('left', parseInt(obj.css('left')) + Xpix);       
    obj.css('top', parseInt(obj.css('top')) + Ypix);
}

功能ProcessKeypress

function ProcessKeypress(e) {
    var myObj = $("#snake"); //It will be better if it is cached
    var moveBy = 10;
    switch(e.charCode) { //used Switch for better manageability
        case 97: moveObj(myObj, -moveBy, 0); break;
        case 100: moveObj(myObj, moveBy, 0); break;
        case 115: moveObj(myObj, 0, moveBy); break;
        case 119: moveObj(myObj, 0, -moveBy); break;            
    }
}

而是将事件触发器附加到文档

$(document).on('keypress', ProcessKeypress);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM