简体   繁体   English

FadeIn FadeOut以及为什么div会移动?

[英]FadeIn FadeOut and why the div moves?

probably is something that I'm missing, but I have a little headache with this. 可能是我缺少的东西,但我有点头疼。

I'd like to have the "submenu div" align on the right of Show/hide links. 我想在显示/隐藏链接的右侧对齐“子菜单div”。

When I load the div is correctly in its place, but when I click to hide/show links, suddenly the div changes the place to the bottom. 当我加载div正确的位置,但当我点击隐藏/显示链接时,突然div将位置更改为底部。

BTW, is there any other better way to do this, or this this is good? 顺便说一下,有没有其他更好的方法来做到这一点,或者这是好事? Also, if I don't what to show the div on the page load, I'm thinking to use .hide() or hidden style, is that ok? 另外,如果我没有在页面加载上显示div的内容,我想使用.hide()或隐藏的样式,那可以吗?

Example http://jsfiddle.net/DH75T/ 示例http://jsfiddle.net/DH75T/

Thanks in advance 提前致谢

CSS CSS

div.inline2 {
    display: inline-block;
    width: 150px;
}
div.inline {
    position:absolute;
    display: inline-block;
    border:1px solid #CCC;
    background:#FFF;
} 

JS JS

$(document).ready(function() {  

  $('a#show').click(function() {
    $('div#submenu').fadeIn();
  });

  $('a#hide').click(function() {
    $('div#submenu').fadeOut();
  });
});

HTML HTML

  <div class="inline2">
    <a href="#" id="show">Show_links</a>
    <a href="#" id="hide">Hide links</a>
  </div>

  <div class="inline" id="submenu">
    <a href="#">Link 1</a><br />
    <a href="#">Link 2</a>
  </div>

fadeIn() adds div style display: block; fadeIn()添加div样式display: block; so div shows down to next line 所以div显示下一行


Before div was styled inline-block 之前div被称为inline-block

div.inline2 {
    display: inline-block;
    width: 150px;
}


fiddle Demo 小提琴演示

Use classes to add effect of fadeIn and fadeOut without moving your div to next line 使用类添加fadeInfadeOut效果,而不将div移动到下一行

 $(document).ready(function () { $('a#show').click(function () { $('div#submenu').removeClass('hidden').addClass('visible'); }); $('a#hide').click(function () { $('div#submenu').addClass('hidden').removeClass('visible'); }); }); 

css CSS

 .visible { opacity: 1; transition: opacity 2s linear; } .hidden { visibility: hidden; opacity: 0; transition: visibility 0s 2s, opacity 2s linear; } 

Try: 尝试:

$(document).ready(function() {          
  $('a#show').click(function() {
    $('div#submenu').removeClass("none");
  });    
  $('a#hide').click(function() {
    $('div#submenu').addClass("none");
  });
});

Fiddle here. 在这里小提琴

You need to change only jQuery code :) : 你只需要改变jQuery代码:):

<script type="text/javascript">
  $(document).ready(function() {  

$('a#show').click(function() {
$( "div#submenu" ).animate({
    opacity: 1
  }, 500, function() {
    // Animation complete.
  });
});

$('a#hide').click(function() {
$( "div#submenu" ).animate({
    opacity: 0
  }, 500, function() {
    // Animation complete.
  });
});
});</script>

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

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