简体   繁体   English

Javascript Animate打开/关闭菜单

[英]Javascript Animate Open/Close Menu

I'm working on a simple Open/Close Animate DIV for a webpage I'm building. 我正在为正在构建的网页开发一个简单的“打开/关闭动画DIV”。 It should be simple rather, but something is giving me a hangup. 它应该很简单,但是却给我带来了麻烦。 Heres my JS code: 这是我的JS代码:

<script>
slideOpen = 1;
alert(slideOpen);

function openClose(){

if (slideOpen = 1){
    closeTab();
    slideOpen = 0;
};
if (slideOpen = 0){
    openTab();
    slideOpen = 1;
};
function closeTab(){
    $("#upcomingEvents").animate({top:"-910px"});
}

function openTab(){
    $("#upcomingEvents").animate({top:"-300px"});
}
alert(slideOpen);
}
</script>

And here's the HTML that runs the function: 这是运行该功能的HTML:

<a href="javascript:openClose();" style="position: absolute; bottom: 32px; right: 32px;" >Open/Close Tab</a>

So, the idea behind this was that whenever a user clicks the anchor, it runs the openClose() function. 因此,其背后的想法是,只要用户单击锚点,它就会运行openClose()函数。 On initial page load, slideOpen = 1, if a user clicks the anchor, if slideOpen = 1, it runs the closeTab() animation function and then sets slideOpen = 0. If slideOpen = 0, the openTab() animate function should run and set slideOpen = 1. 在初始页面加载时,slideOpen = 1,如果用户单击锚点,如果slideOpen = 1,则它将运行closeTab()动画函数,然后将slideOpen = 0设置。如果slideOpen = 0,则应运行openTab()动画函数并设置slideOpen = 1。

Basically: If tab is open slideOpen = 1; 基本上:如果选项卡是打开的,slideOpen = 1; if tab is closed slideOpen = 0; 如果选项卡关闭,则slideOpen = 0;

However, I've also set two alerts in this function; 但是,我还在此功能中设置了两个警报。 an initial one on page load, and one that runs every time the openClose() function is called. 最初的页面加载,每次调用openClose()函数时运行。

On inital page load, slideOpen alert says,"1". 初始页面加载时,slideOpen警报显示“ 1”。 After clicking the anchor to close the tab, slideOpen alert in the openClose() function says, "0" and the tab closes successfully. 单击锚点以关闭选项卡后,openClose()函数中的slideOpen警报显示“ 0”,并且选项卡成功关闭。 Clicking the anchor again, the alert says, "0" again and the tab does not re-open. 再次单击锚点,警报将再次显示“ 0”,并且该选项卡不会重新打开。 It SHOULD display slideOpen = "1", but for some reason it's not. 它应该显示slideOpen =“ 1”,但是由于某种原因,它不是。 Therein lies my problem. 这就是我的问题。

Thanks in advance. 提前致谢。

if (slideOpen = 1){

Here you are assigning 1 to variable slideOpen , so the condition will always evaluate to true. 在这里,您将1分配给变量slideOpen ,因此条件将始终为true。

use == instead of = for both conditions 两种情况都使用==代替=

like 喜欢

if (slideOpen == 1){

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

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