Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - Troubles with JS code

I have troubles with code which must display the image within a frame 300x300px. Also there must be an ability to zoom in and out image inside the frame by pressing the + and - on the keyboard. I'm a mac user, so maybe the key codes for + & - is not correct, or I asked the wrong path to the image (it's in the same folder as script file). Please, help and thanks for watching! Maybe there is more elegant method to do this task...

<html>
<head>
    <script type="text/javascript">
    var speed = 4;
function showImage (src)
{
    var div = document.createElement ("div");
    with (div.style)
    {
        width = "300px";
        heigth = "300px";
        border = "2px solid black";
        textAlign = "center";
        overflow = "hidden";
    }
    document.body.appendChild(div);
    img = document.createElement ("img");
    img.src = src;
    img.width = 300;
    img.height = 300;
    div.appendChild (img);
}
function keyDown (key);
{
    var k = 0;
    if (key == 107 ) k = speed;
    if (key == 109 ) k = -speed;
    if (k != 0 )
    {
        img.width = img.width + k;
        img.height = img.height + k;
        img.style.margin = ((300 - img.height) / 2).toString() + "px";
    }
}
</script>
</head>
<body onkeydown="keyDown (event.keyCode)" onload="showImage ('image.jpg')">
</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use your console to debug Javascript. It's the F12 key on Firefox and Chrome, for Safari follow these instructions.

The image does not load because you have a syntax error line 23, a ; at the end of the definition of your keyDown function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...