简体   繁体   English

如何在悬停的div中顺利过渡? [Tumblr更新标签]

[英]How can I smoothly transition in a div on hover? [Tumblr Updates Tab]

I am sure this has been answered before, but I am not sure where to look for it, nor how to really word the question. 我确信这已经得到了回答,但是我不确定在哪里寻找它,也不确定如何表达这个问题。 But basically, I am creating an updates tab for a tumblr theme and I'm curious as to how make a div smoothly transition in while hovering over a parent div, if that makes sense. 但基本上,我正在为tumblr主题创建一个更新选项卡,并且我想知道如何在使div悬停在父div上的同时使div平稳过渡。

Essentially I would like the div to transition in smoothly when I hover over the tab like it does when I click on a tab in my updates tab in my blog . 本质上,当我将鼠标悬停在选项卡上时,就像单击博客的更新选项卡的选项卡一样,div会平滑过渡。 However this tab was created using a tutorial that used a jquery code to create the smooth opening effect, and I don't know anything about jquery or javascript on my own. 但是,此标签是使用使用jquery代码创建平滑打开效果的教程创建的,我自己对jquery或javascript一无所知。

I have the general idea of how to make a div show only when you hover over the parent element by using display:none and display:block ... but it doesn't transition in smoothly, and as I've read there is no way to do so with CSS. 我有一个一般性的想法,即如何仅在使用display:nonedisplay:block ...将鼠标悬停在父元素上时显示div,但是它不会平滑过渡,而且正如我所读到的那样, CSS的方法。

I have tried playing with height and opacity as well, but while it was a bit smoother, it was a rather messy transition with dealing with text and borders of the div I'm trying to show and hide. 我也尝试过使用高度和不透明度,但是虽然它更平滑一些,但是在处理要显示和隐藏的div的文本和边框时,这是一个相当混乱的过渡。

Basically what I am asking is how I can create the same effect in my blog's updates tab that opens and closes when clicking on the tab, but using hover rather than click. 基本上,我要问的是如何在博客的“更新”选项卡中创建相同的效果,该选项卡在单击选项卡时会打开和关闭,但要使用悬停而不是单击。 I would love for the div to show when I'm hovered over the tab, as well as when my mouse is within the containing div, but close once my mouse leaves the tab. 我希望div在将鼠标悬停在选项卡上时以及鼠标在包含div内时显示,但是一旦鼠标离开选项卡就关闭。 Sorry if that sounds confusing. 抱歉,这听起来令人困惑。

Here is the CSS: 这是CSS:

 #tab{ margin:auto; } .tab{ margin-top:0px; margin-bottom:2px; margin-left:-5px; margin-right:-5px; padding:7px; border:1px solid #000000; text-align:center; } #tab:hover .contents{ display:block; } .contents{ display:none; margin-top:3px; margin-bottom:3px; margin-left:10px; margin-right:10px; padding:5px; border:1px solid #000000; text-align:center; font-size:11px; } 

Here is the HTML for that CSS: 这是该CSS的HTML:

  <div id="tab"> <div class="tab">Tab</div> <div class="contents"> Tab Contents. </div> </div> 

Jquery/ or javascript? jQuery /或JavaScript? From the tutorial from my blog's updates tab: 在我博客的“更新”标签中的教程中:

  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $(".inside").hide(); $(".label").click(function(){ $(this).next(".inside").slideToggle('slow'); }); }); </script> 

You can use CSS3 transition-property to add 'smooth' effect. 您可以使用CSS3过渡属性添加“平滑”效果。 The only 'problem' with that , it doesnt work on IE9 and earlier versions. 唯一的“问题”,它不适用于IE9和更早版本。 You can see example of that: here 您可以看到示例: 这里

You can probably do it like this. 您可能可以这样做。 Use opacity instead for the display and transition. 使用不透明度代替显示和过渡。 I added some pointer-event and make it none so once it is hidden, by the opacity, you can't interact it, which act like display: none . 我添加了一些pointer-event ,但没有使pointer-event变为零,因此一旦它被隐藏,由于不透明,您将无法进行交互,其作用类似于display: none

 #tab{ margin:auto; } .tab{ margin-top:0px; margin-bottom:2px; margin-left:-5px; margin-right:-5px; padding:7px; border:1px solid #000000; text-align:center; } #tab:hover .contents{ opacity: 1.0 } .contents{ opacity: 0.0; margin-top:3px; margin-bottom:3px; margin-left:10px; margin-right:10px; padding:5px; border:1px solid #000000; text-align:center; font-size:11px; transition: opacity 100ms ease-in-out; pointer-events: none; } 

As an alternative, if the CSS3 transition does not work, you can try this: 或者,如果CSS3转换不起作用,则可以尝试以下操作:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script> 
$(document).ready(function(){ 
  $(".inside").hide(); 
  $(".label").click(function(){ 
    $(this).next(".inside").slideToggle('slow'); 
  }); 

  $(document).on("hover", ".label", function(){
    $(this).next(".inside").slideToggle('slow'); 
  });
}); 
</script>

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

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