javascript - javascript自定义脚本中的淡入淡出效果属性/函数?

标签 javascript html css

我需要在我的 javascript 函数上添加淡入淡出效果

Javascript

<script type="text/javascript">
window.onload=function() {
  loginBtn = document.getElementById('loginBtn');
  fader = document.getElementById('login_fader');
  login_box = document.getElementById('login_box');
  closebtn = document.getElementById('closelogin');
  loginBtn.onclick=function(){
    fader.style.display = "block";
    login_box.style.display = "block";
  }
  closebtn.onclick=function() {
    fader.style.display = "none";
    login_box.style.display = "none";
  }
}
</script>

HTML

<div id="login_fader">&nbsp;</div>
<div id="login_box">
  <table class="table-login">
    <th>Login or Register</th>
    <th><a id="closelogin">X</a></th>
    <tr>
      <td>Login</td>
  <td>Register</td>
    </tr>
  </table>
</div>

CSS

<style type="text/css">
  #loginBtn {
    float: right;
    margin-top: -6%;
    cursor:pointer;
  }
  #login_fader {
   background: black;
   opacity: .5;
   -moz-opacity: .5;
   -filter: alpha(opacity=50);
   position: fixed;
   top: 0;
   left: 0;
   height: 100%;
   width: 100%;
   z-index: 5;
   display: none;
  }
#login_box {
  width: 320px;
  height: 200px;
  border: 1px white solid:
  background: #5a5a5a;
  position: fixed;
  top: 25%;
  left: 35%;
  z-index: 10;
  display: none;
}

.table-login {
  background: #FFF;
  border-radius: 8px;
  box-shadow: 0 0 5px 2px;
  opacity: 0.95;
}

#closelogin {
  float:right;
  cursor:pointer;
}
</style>

JS fiddle

http://jsfiddle.net/U3n4j/

我已经尝试使用 css3 中的 transition 属性,并尝试将两者应用于 login_boxlogin_fader。 我在网上找到了一些函数,但不知道如何将它们链接到我已经创建的函数,我在想是否有任何属性可以直接将它们链接到我的函数。

最佳答案

在 css3 和 js 1.7++ 中淡入静态框的正确方法

这是一个仅使用 webkit 和现代 javascripts classList.add

的示例

但您可以添加其他前缀。-moz,-ms,-o

在这个例子中我只展示了动画。

CSS

.box{
 width:100%;
 height:100%;
 position:fixed;
 left:0;top:-100%;/*notice TOP -100%*/
 opacity:0;
 -webkit-transition:opacity 700ms ease,top 0 linear 700ms;/*notice TOP delay*/
 background-color:rgba(0,0,0,0.7);
}
.box.active{
 -webkit-transition:opacity 700ms ease,top 0 linear 0;
 /*top transition not needed but could help to understand*/
 top:0;
 opacity:1;
}

js

function show(){
 box.classList.add('active');
}
function hide(){
 box.classList.remove('active');
}
var box=document.getElementsByClassName('box')[0],
button=document.getElementsByTagName('button')[0];
button.addEventListener('click',show,false);
box.addEventListener('click',hide,false);

演示

http://jsfiddle.net/RAu8Q/不再工作了

http://jsfiddle.net/RAu8Q/17/ 新语法 10-2015

如果您有任何问题,请尽管提问。

关于javascript - javascript自定义脚本中的淡入淡出效果属性/函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21939042/

相关文章:

javascript - 如何在 onmouseup 上获取坐标?

javascript - 带有 base64 字符串(数据 URI)的 HTML 图像标签

html - 导航栏呈现在主容器上方而不是上方

javascript - 如果相同的功能已经在运行,则什么都不做

javascript - 如何在 Impress.js 上聚焦幻灯片时取消视频静音

javascript - 如何创建带背景重复的梯形div

javascript - 控制 Jquery scrolltop 速度

jquery - CSS 在浏览器中未处于事件状态时未更新?

javascript - 鼠标在jquery菜单中移出,隐藏子菜单

javascript - AngularJS orderBy 使用字典顺序,即使是数字