图像功能上的Box-Shadow不起作用

时间:2018-05-18 12:49:29

标签: javascript html css onclick shadow

无法绕过这一个,为什么每次单击时图像上的onclick功能都不会掉落阴影?你能帮忙吗,我是一名自学程序员,会很感激。 (看看我的Javascript文件)。

最终结果应该像这样https://jenniferdewalt.com/more_drop_shadow.html,但是她在jQuery中写了它,我还不知道这个框架

CODE



    
    var newBlur = 0;
    var newSpread = 0;

document.getElementById("catIMG").addEventListener("click", shadowIMG);

function shadowIMG() {
    var myShadow = genShadow();
    document.getElementById("catIMG").style.boxShadow = myShadow;
    console.log(myShadow);
}

function genShadow() {
    var arr = [0, 0, newBlur, newSpread,];
    newBlur +=1;
    newSpread +=5;
    var newShadow = '"' + arr[0] + 'px ' + arr[1] + 'px ' + arr[2] + 'px ' + arr[3] + 'px ' + 'Black' + '"' ;
    return newShadow;
}

body
{
    margin: 0;
    background-color: lightgrey;
}

input {
    position: absolute;
    left: 10%;
    width: 100px;
    height: 50px;
    font-size: 20px;
    color: white;
    background-color: green;
}

#blueTop {
    font-size: 30px;
    font-family: Arial;
    width: 100%;
    height: 100px;
    background-color: mediumpurple;
}


#catIMG {
    position: absolute;
    left: 50%;
    top: 15%;
    transform: translate(-50%);
    width: 750px;
    height: 500px;
    cursor: pointer;

}

#catText {
    position: absolute;
    left: 50%;
    top: 800px;
    transform: translate(-50%);
    width: 750px;
    text-align: center;
    font-size: 50px;
    font-weight: 100;
    color: rgb(50,20,0);
    cursor: pointer;
}

h5 {
    position: absolute;
    left: 50%;
    top: 1000px;
    transform: translate(-50%);
    font-size: 25px;
    cursor: pointer;
}

<form>
<input id="testButton"type="button" value="Test"/>
</form>
<div id="blueTop"></div>
<img id="catIMG" src="./more_grumpy_shadow.png"/>
<h1 id="catText">"You will always be lucky if you know how to make friends with strange cats."</h1>
<h5>- Colonial proverb</h5>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

问题出在这一行:

var newShadow = '"' + arr[0] + 'px ' + arr[1] + 'px ' + arr[2] + 'px ' + arr[3] + 'px ' + 'Black' + '"' ;

您正在添加额外的报价。

// JavaScript source code

var newBlur = 2;
var newSpread = 2;



//functions
document.getElementById("catIMG").addEventListener("click", shadowIMG);

function shadowIMG() {
    var myShadow = genShadow();
    document.getElementById("catIMG").style.boxShadow = myShadow;
    debugger;
    console.log(myShadow);
}

function genShadow() {
    var arr = [10, 10, 10, 10,];
    newBlur +=1;
    newSpread +=5;
    var newShadow = '' + arr[0] + 'px ' + arr[1] + 'px ' + arr[2] + 'px ' + arr[3] + 'px ' + ' black' + '' ;
    console.log(newShadow);
    return newShadow;
}
body
{
    margin: 0;
    background-color: lightgrey;
}

input {
    position: absolute;
    left: 10%;
    width: 100px;
    height: 50px;
    font-size: 20px;
    color: white;
    background-color: green;
}

#blueTop {
    font-size: 30px;
    font-family: Arial;
    width: 100%;
    height: 100px;
    background-color: mediumpurple;
}


#catIMG {
    position: absolute;
    left: 50%;
    top: 15%;
    transform: translate(-50%);
    width: 750px;
    height: 500px;
    cursor: pointer;

}

#catText {
    position: absolute;
    left: 50%;
    top: 800px;
    transform: translate(-50%);
    width: 750px;
    text-align: center;
    font-size: 50px;
    font-weight: 100;
    color: rgb(50,20,0);
    cursor: pointer;
}

h5 {
    position: absolute;
    left: 50%;
    top: 1000px;
    transform: translate(-50%);
    font-size: 25px;
    cursor: pointer;
}
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <form>
            <input id="testButton"type="button" value="Test"/>
        </form>
        <div id="blueTop"></div>
            <img id="catIMG" src=""/>
            <h1 id="catText">"You will always be lucky if you know how to make friends with strange cats."</h1>
            <h5>- Colonial proverb</h5>

    </body>
    </html>

答案 1 :(得分:0)

你不需要做额外的工作。每当用户点击按钮时,使用javascript样式属性增加blur-radiusspread-radius即可轻松完成。

var i = 0;
var j = 3;
document.getElementById("makeshadow").addEventListener("click", function() {
    i++;
    j = i * 3;
   document.getElementById("catIMG").style.boxShadow  = 
   "rgba(0, 1, 0, 0.35) 0px 0px "+i+"px "+j+"px";
});
body
{
    margin: 0;
   padding:20px;
}
 <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link href="./dropShadow.css" type="text/css" rel="stylesheet"/>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        
           
  

            <img id="catIMG" src="http://via.placeholder.com/350x150"/>
      
<button id="makeshadow">Button</button>

    </body>
    </html>

答案 2 :(得分:0)

删除变量newShadow上的引号解决了问题,谢谢所有帮助过我的人。