简体   繁体   English

如何使HTML中的框过渡更平滑?

[英]How to make a box transition smoother in html?

I've been trying to find some information about this, but I cant find some good information. 我一直在寻找有关此的一些信息,但是我找不到一些好的信息。
I want to make a div, that says "Contact Us" and when you click on the div, a layer shows up smoothy with input types. 我要创建一个div,上面写着“与我们联系”,当您单击div时,一个图层显示的是输入类型。

I know that I can make some quick javascript to change from display:none to display:block, but how can I do it smooth? 我知道我可以使一些快速的javascript从display:none更改为display:block,但是如何使它平滑?

for an example (just a quick example, the actual one will be better) 举一个例子(只是一个简单的例子,实际的会更好)

<div id="contact-us" onClick="showContactUs()">
<div id="contact-us-content" style="display: block;">
  Name - <input type="text" name="name">
  Email- <input type="text" name="email">
</div>
</div>

And then javascript is 然后javascript是

function showContactUs(){
   var r = document.getElementById("contact-us-content");
   r.style.display = "block";
}

If any of you have any tips, or a link I can check I would appreciate it. 如果您有任何提示或链接,我可以检查一下,不胜感激。 I am not that good with jquery, but can absolutely try some if you think its better. 我对jquery不太满意,但是如果您认为更好,可以绝对尝试一些。

There is no way to make the appearing smooth by setting display: block . 无法通过设置display: block来使外观平滑。 You can, however, transition opacity . 但是,您可以转换不透明 I suggest adding a class by javascript and solving the rest by css. 我建议通过javascript添加一个类,并通过CSS解决其余的问题。

Check it out here: https://jsfiddle.net/3wLrfk3d/ 在这里查看: https : //jsfiddle.net/3wLrfk3d/

$(document).on('click', '#contact-us', show_contact_form)

function show_contact_form () {
    $('#contact-us-content').addClass('shown')
}

css: CSS:

#contact-us-content {
    opacity: 0;
    transition: opacity .5s ease;

    &.shown {
        opacity: 1;
    }
}

My example uses jquery and sass, I am sure you will be able to rewrite it to vanilla and css. 我的示例使用jquery和sass,我确信您将能够将其重写为vanilla和css。

I usually transition the opacity, but that will mean the element will be there even if it's invisible, taking up space and blocking mouse events. 我通常会转换不透明度,但这将意味着该元素即使在不可见的情况下也将存在,这会占用空间并阻止鼠标事件。 I solve this by having two classes, one to fade the element and one to hide it completely when it's done fading out: 我通过有两个类来解决此问题,一个类使元素淡入淡出,而另一类则在淡出时完全隐藏它:

 $(function() { var $box = $('.box'); $('.toggle').on('click', function() { if (!($box).hasClass('visible')) { $box.addClass('transitioning'); setTimeout(function() { $box.addClass('visible'); }, 1) } else { $box.removeClass('visible'); setTimeout(function() { $box.removeClass('transitioning'); }, 400) } }) }) 
 body { font-family: Helvetica, Arial, sans-serif; } .box { background-color: #333; color: white; border: 5px solid white; padding: 50px 10px; box-shadow: 0 0 10px 0 rgba(0, 0, 0, .4); text-align: center; transition: opacity .4s ease-in-out; display: none; opacity: 0; } .transitioning { display: block; } .visible { opacity: 1; } .toggle { margin-bottom: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="toggle">Toggle Box</button> <div class="box"> <h1>Hi there</h1> </div> 

Note: this is a quick and dirty example, it kinda freaks out if you spam clicks on the toggle button, I'll leave that up to you. 注意:这是一个简单而肮脏的示例,如果您在垃圾邮件单击切换按钮时有点奇怪,我将全由您决定。

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

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