javascript - Div 只移动一次

标签 javascript css html

我正在尝试通过英特尔 XDK 制作一个简单的应用程序。我做了这个按钮,但它只在加载页面/应用程序后向下移动一次。如果有人给我建议,我将不胜感激。提前致谢。

代码:

function buttonClick() {
    //alert('Thanks for using our app!!!');
    document.getElementById('button').style.boxShadow = "none";
    document.getElementById('button').style.top += "10px";
    setTimeout(function(){ 
      document.getElementById('button').style.top = "-10px";
        document.getElementById('button').style.boxShadow = "0px 5px 10px #3a926d";
        }, 500);
    }
    
function onLoad() {
    intel.xdk.device.setRotateOrientation("portrait");
}
    
document.addEventListener("intel.xdk.device.ready", onDeviceReady, false);               
function onDeviceReady(){
    // set orientation
    
    intel.xdk.device.setRotateOrientation('portrait');
//    intel.xdk.device.setRotateOrientation('any');

    intel.xdk.device.hideSplashScreen();   
} 
#button {
    background-color: #29c786;
    padding: 10px;
    width: 55%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    margin-top: 0;
    color: white;
    position:absolute; /*it can be fixed too*/
    left:0; right:0;
    top:0; bottom:0;
    margin:auto;
    box-shadow: 0px 5px 10px #3a926d;
    border-radius: 5px;
}

footer {
    font-size: 8;
    position: absolute;
    bottom: 5px;
    left: 5px;
}
<div id="button" onClick="buttonClick()"><b>OPEN GALLERY</b></div>
<footer>VERSION 0.1; &copy; Tomas Hobza 2017</footer>

最佳答案

此类任务不需要 JavaScript。

这是一个 CSS 解决方案:

CSS

#button {
  background-color: #29c786;
  padding: 10px;
  width: 55%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  margin-top: 0;
  color: white;
  position:absolute;
  left:0; right:0;
  top:0; bottom:0;
  margin:auto;
  box-shadow: 0px 5px 10px #3a926d;
  border-radius: 5px;
  transition: all 0.5s;
  font-family: Arial, Helvetica, sans-serif;
}

#button:active {
  transform: translateY(10px);
  box-shadow: none;
}
<div id="button"><b>OPEN GALLERY</b></div>

JSFiddle

关于javascript - Div 只移动一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44415135/

相关文章:

css - XS 设备上从容器到容器流体

javascript - 如何使用javascript for循环和if语句在右表中显示数据?

jquery - 如何在单击事件之前隐藏菜单中的元素?

javascript - IOS 上的推送状态/弹出状态问题 - 历史 api/javascript

javascript - 如何在 javascript 中访问嵌套的 html-child 元素

javascript - 使用 window.open 打开多个 URL

javascript - CSS 中的按钮位置

Jquery创建下推div效果

jquery - 使用 jQuery 的垂直和水平对齐(chrome 和 safari)

javascript - jQuery 警报代码不起作用