简体   繁体   English

jQuery幻灯片切换

[英]JQuery slidetoggle

I am currently building a website for an indie game development team I am working with. 我目前正在为与之合作的独立游戏开发团队建立网站。

Right now I am writing the alpha sign up page and I have created layout which needs to make use of the slideToggle() and fadeToggle() methods, however after a few hours of faffing around in my editor I cannot seem to fathom a solution for the behavior I want. 现在,我正在编写Alpha注册页面,并且已经创建了需要使用slideToggle()和fadeToggle()方法的布局,但是在编辑器中忙了几个小时之后,我似乎无法找到解决方案我想要的行为。

When the page loads I want the form container to be in its slid up position and when the user clicks on the div I want the form container to animate down. 当页面加载时,我希望表单容器处于向上滑动的位置,并且当用户单击div时,我希望表单容器向下动画。

I have tried to animate the display property from hidden to block and I have also tried to hide the element when the document loads and then re-show it on click, however when I did that I noticed a strange behavior as the div would slide down and then up, which would cause the user to have to press the button again to view the sign-up form. 我尝试将显示属性从隐藏设置为动画,也尝试在文档加载时隐藏元素,然后在单击时重新显示它,但是当我这样做时,我注意到一个奇怪的行为,因为div会向下滑动然后向上,这将导致用户不得不再次按下按钮才能查看注册表单。

The code below handles the click event, 下面的代码处理click事件,

    <a href="#" >
      <div onclick="$('#showForm .inner').fadeToggle({queue:false});$('#showForm').slideToggle();" id="alpha-container" class="alpha-off"></div>
    </a>

And this is the div I want to be hidden and re-appear 这是我要隐藏并重新出现的div

<div id="formContainer" class="form container">
        <div id="showForm" class="outer">
            <div class="inner">
                <form method="post" action="verify.php">
                    <table>
                        <tr>
                            <td class="intro">
                                <h1 class="header-orange">Liberico Alpha Application</h1>
                                <p class="body-text">So you think you have what it takes to brave the battlefield?
                                </p>
                            </td>
                        </tr>

                    </table>
                </form>
            </div>
      </div>
</div>

I have created a jsfiddle demonstrating how my page currently renders - http://jsfiddle.net/alexmk92/54EUC/ - any pointers would be greatly appreciated. 我创建了一个jsfiddle来演示页面当前的呈现方式-http: //jsfiddle.net/alexmk92/54EUC/-任何指针都将不胜感激。

Firstly, i removed the javascript code from html and added a class "clickable" to the clickable element: 首先,我从html中删除了javascript代码,并向clickable元素添加了一个“ clickable”类:

<a class="clickable" href="#" >
          <div  id="alpha-container" class="alpha-off">CLICK ME FOR ANIMATION</div>
        </a>

And then, i've created a custom javascript toggle function with slideDown and up: 然后,我使用slideDown和up创建了一个自定义javascript切换功能:

$('.clickable').click(function(){    
    var showFormObj = $('#showForm');

    if(showFormObj.hasClass('active')){
        showFormObj.removeClass('active');
        showFormObj.slideUp();
    }else{
        showFormObj.addClass('active');
        showFormObj.slideDown();
    }

});

On the css part i hid the 'showForm' element: 在css部分,我隐藏了'showForm'元素:

#showForm {display:none;}

Here is the fiddle: http://jsfiddle.net/Karzin/54EUC/2/ 这是小提琴: http : //jsfiddle.net/Karzin/54EUC/2/

If you need to hide the form when page loads. 如果在页面加载时需要隐藏表单。 For form container use 用于表格容器

display : none

Eg: 例如:

http://jsfiddle.net/54EUC/1/ http://jsfiddle.net/54EUC/1/

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

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