简体   繁体   English

单击链接后使用Java的Div动画-怎么做?

[英]Div animation with Javascript after clicking on a link - how?

I would like to animate a div after clicking on a link. 单击链接后,我想为div设置动画。 When you click on "About", the should change its height. 当您点击“关于”时,应当更改其高度。 Height is set to '0em' in the CSS file. CSS文件中的高度设置为“ 0em”。 It works if I write "document.getElementById('about-div').style.height = '';", but it will jump suddenly, not animated. 如果我编写“ document.getElementById('about-div')。style.height =”;”,它将起作用,但是它会突然跳动,而不是动画。 Now I tried this code but unfortunately, it doesn't want to work: 现在,我尝试了这段代码,但不幸的是,它不想工作:

CSS: CSS:

.about-div {position:absolute;top:100vh;left:0em;width:100vw;height:0em;z-index:10000;background:white;overflow:hidden;}

.open {
  -webkit-animation: open 1s ease-in 1 forwards;
  animation: open 1s ease-in 1 forwards;
}

.is-paused {
  -webkit-animation-play-state: paused;
  animation-play-state: paused;
}

@keyframes open {
  0%   {height:0em;}
  1%     {height:'';}
  100% {height:'';}
}

@-webkit-keyframes open {
  0%   {height:0em;}
  1%     {height:'';}
  100% {height:'';}
}

@keyframes close {
  0%   {height:'';}
  100% {height:0em;}
}

@-webkit-keyframes close {
  0%   {height:'';}
  100% {height:0em;}
}

HTML: HTML:

<a href="javascript:openAbout()">About</a>

<div class="about-div open is-paused">
   <p>Some Content</p>
</div>

...and the Javascript code: ...以及Javascript代码:

function openAbout()  {
  <script>
    document.querySelector('.about-div').classList.remove('is-paused');
  </script>
}

Any idea about it? 有什么想法吗?

Please see my fiddle . 请看我的小提琴

You should remove the script tags from inside your JS function. 您应该从JS函数内部删除script标签。 All parts of your JS should be inside the script tags like below. JS的所有部分都应位于以下脚本标记内。

<script>
function openAbout() {
    document.querySelector('.about-div').classList.remove('is-paused');
}
</script>

Also, I think using animation to animate the height is a bit cumbersome, so I used transition instead. 另外,我认为使用动画为高度设置动画有点麻烦,因此我改用了过渡。 Please see demo below (I removed the bottom positioning for easier viewing, you can put it back when you use it). 请参阅下面的演示(为了方便查看,我删除了底部位置,可以在使用时放回去)。

 function openAbout() { document.querySelector('.about-div').classList.remove('is-paused'); } 
 body { background: #ddd; } .about-div { position:absolute; left:0em; width:100vw; z-index:10000; background:white; overflow:hidden; transition: max-height 5s; } .open { max-height: 999px; } .is-paused { max-height: 0px; } 
 <a href="#" onclick="openAbout()">About</a> <div class="about-div open is-paused"> <p>Some Content</p> </div> 

The function needs to be in script tags, not script tags inside the function. 该功能必须位于脚本标签中,而不是功能内部的脚本标签中。

<script>
function openAbout()  {
    document.querySelector('.about-div').classList.remove('is-paused');
}
</script>

At first: 首先:

'href' isn't intended for calling functions. “ href”不适用于调用函数。 Most HTML elements have an 'onclick' tag for doing that. 大多数HTML元素都具有一个“ onclick”标记。

<a href="#" onclick="openAbout()">About</a>

And you need to remove the '' tags like mentioned so your code looks like: 而且您需要删除如前所述的''标签,因此您的代码应如下所示:

function openAbout() {
    document.querySelector('.about div').classList.remove('is-paused');
}

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

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