When you click the login or registration button on the page, a modal window will pop up, which is a pop-up layer. We can easily switch the login and registration forms on the pop-up layer, which is very convenient for users and does not need to be closed. Click on the layer to switch to other operations, which has been widely used on many websites.
This article combines examples to achieve this effect by using jQuery as well as CSS3 and HTML5 technologies.
HTML
We now have two link buttons on the main page, namely the login and registration buttons.
<nav class="main_nav"> <ul> <li><a class="cd-signin" href="#0">登录</a></li> <li><a class="cd-signup" href="#0">注册</a></li> </ul> </nav>
Then, create a modal window pop-up layer div.cd-user-modal, place two links for switching ul.cd-switcher in the pop-up layer, and then place the login and registration forms, corresponding to div#cd- login and div#cd-signup.
<div class="cd-user-modal"> <div class="cd-user-modal-container"> <ul class="cd-switcher"> <li><a href="#0">用户登录</a></li> <li><a href="#0">注册新用户</a></li> </ul> <div id="cd-login"> <form class="cd-form"> <!-- 登录表单 --> </form> </div> <div id="cd-signup"> <form class="cd-form"> <!-- 注册表单 --> </form> </div> </div> </div>
The above is the entire html structure, and the form part is omitted here. You can freely write your form structure according to your needs. You can also directly download and view the source code.
CSS
The default modal window has the styles of visibility: hidden; and opacity: 0;, which means it is invisible by default. Use .is-visible to decide whether to pop up the display. The following is the main css code. For more detailed css code, please download the source code to view.
.cd-user-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(52, 54, 66, 0.9); z-index: 3; overflow-y: auto; cursor: pointer; visibility: hidden; opacity: 0; -webkit-transition: opacity 0.3s 0, visibility 0 0.3s; -moz-transition: opacity 0.3s 0, visibility 0 0.3s; transition: opacity 0.3s 0, visibility 0 0.3s; } .cd-user-modal.is-visible { visibility: visible; opacity: 1; -webkit-transition: opacity 0.3s 0, visibility 0 0; -moz-transition: opacity 0.3s 0, visibility 0 0; transition: opacity 0.3s 0, visibility 0 0; } .cd-user-modal.is-visible .cd-user-modal-container { -webkit-transform: translateY(0); -moz-transform: translateY(0); -ms-transform: translateY(0); -o-transform: translateY(0); transform: translateY(0); } .cd-user-modal-container { position: relative; width: 90%; max-width: 600px; background: #FFF; margin: 3em auto 4em; cursor: auto; border-radius: 0.25em; -webkit-transform: translateY(-30px); -moz-transform: translateY(-30px); -ms-transform: translateY(-30px); -o-transform: translateY(-30px); transform: translateY(-30px); -webkit-transition-property: -webkit-transform; -moz-transition-property: -moz-transform; transition-property: transform; -webkit-transition-duration: 0.3s; -moz-transition-duration: 0.3s; transition-duration: 0.3s; } .cd-user-modal-container .cd-switcher:after { content: ""; display: table; clear: both; } .cd-user-modal-container .cd-switcher li { width: 50%; float: left; text-align: center; } .cd-user-modal-container .cd-switcher li:first-child a { border-radius: .25em 0 0 0; } .cd-user-modal-container .cd-switcher li:last-child a { border-radius: 0 .25em 0 0; } .cd-user-modal-container .cd-switcher a { display: block; width: 100%; height: 50px; line-height: 50px; background: #d2d8d8; color: #809191; } .cd-user-modal-container .cd-switcher a.selected { background: #FFF; color: #505260; } #cd-login, #cd-signup { display: none; } #cd-login.is-selected, #cd-signup.is-selected{ display: block; }
jQuery
The pop-up and closing effects of the pop-up layer are controlled by jQuery using the style.is-visible call, and the switching form is controlled by jQuery using the demo.is-selected call.
jQuery(document).ready(function($){ var $form_modal = $('.cd-user-modal'), $form_login = $form_modal.find('#cd-login'), $form_signup = $form_modal.find('#cd-signup'), $form_modal_tab = $('.cd-switcher'), $tab_login = $form_modal_tab.children('li').eq(0).children('a'), $tab_signup = $form_modal_tab.children('li').eq(1).children('a'), $main_nav = $('.main_nav'); //弹出窗口 $main_nav.on('click', function(event){ if( $(event.target).is($main_nav) ) { // on mobile open the submenu $(this).children('ul').toggleClass('is-visible'); } else { // on mobile close submenu $main_nav.children('ul').removeClass('is-visible'); //show modal layer $form_modal.addClass('is-visible'); //show the selected form ( $(event.target).is('.cd-signup') ) ? signup_selected() : login_selected(); } }); //关闭弹出窗口 $('.cd-user-modal').on('click', function(event){ if( $(event.target).is($form_modal) || $(event.target).is('.cd-close-form') ) { $form_modal.removeClass('is-visible'); } }); //使用Esc键关闭弹出窗口 $(document).keyup(function(event){ if(event.which=='27'){ $form_modal.removeClass('is-visible'); } }); //切换表单 $form_modal_tab.on('click', function(event) { event.preventDefault(); ( $(event.target).is( $tab_login ) ) ? login_selected() : signup_selected(); }); function login_selected(){ $form_login.addClass('is-selected'); $form_signup.removeClass('is-selected'); $form_forgot_password.removeClass('is-selected'); $tab_login.addClass('selected'); $tab_signup.removeClass('selected'); } function signup_selected(){ $form_login.removeClass('is-selected'); $form_signup.addClass('is-selected'); $form_forgot_password.removeClass('is-selected'); $tab_login.removeClass('selected'); $tab_signup.addClass('selected'); } });
This example also has a good display effect on mobile devices such as mobile phones. Due to the use of css3 effects, if you use IE browser, please upgrade the version to IE9 or above. It is strongly recommended that you download the source code and make some slight changes to directly apply it to your project.
The above is the entire content of this article, I hope you all like it.