父级的动画高度隐藏选择选项

发布于 2024-08-18 03:13:16 字数 1504 浏览 2 评论 0原文

当我专注于选择框时,我希望显示隐藏的工具提示。当我单击选择框时,动画开始,但选项列表被隐藏。我该如何解决这个问题?

<style>
.showme {display:none;}
li {height:25px;background:red; }
select{z-index:100;}
p{margin:0px;padding:0px;}
</style>
<script type="application/javascript">
$(document).ready(function() {

    $("select")
        .focus(function(){
            var myParent = $(this).parent("li");
            var myInfo = $(this).siblings(".showme");

            $(myParent).data("myoldheight", $(myParent).height());

            $(myInfo).css("display","block");                               
            var totalHeight = $(myParent).height() + $(myInfo).height();

            $(myParent).animate({
                "height": totalHeight + "px"                               
            },3000,function() {console.log("animated");})               
        })

        .blur(function() {

            $($(this).parent("li")).animate({
                "height": $(this).parent("li").data("myoldheight") + "px"                              
            },500) 

            $(this).siblings(".showme").hide();
        })
        })
</script>
<form>
<ul>
<li>
<label for="test">Test</label>
<select id="test" name="test">
    <option value=""></option>
    <option value="a">a</option>
    <option value="b">b</option>
</select>
<p class="showme">This is my text</p>    
</li>
</ul>
</form>

When I focus on a select box I want the hidden tooltip to appear. When I click on the select box the animation starts but the option list is hidden. How I get round this?

<style>
.showme {display:none;}
li {height:25px;background:red; }
select{z-index:100;}
p{margin:0px;padding:0px;}
</style>
<script type="application/javascript">
$(document).ready(function() {

    $("select")
        .focus(function(){
            var myParent = $(this).parent("li");
            var myInfo = $(this).siblings(".showme");

            $(myParent).data("myoldheight", $(myParent).height());

            $(myInfo).css("display","block");                               
            var totalHeight = $(myParent).height() + $(myInfo).height();

            $(myParent).animate({
                "height": totalHeight + "px"                               
            },3000,function() {console.log("animated");})               
        })

        .blur(function() {

            $($(this).parent("li")).animate({
                "height": $(this).parent("li").data("myoldheight") + "px"                              
            },500) 

            $(this).siblings(".showme").hide();
        })
        })
</script>
<form>
<ul>
<li>
<label for="test">Test</label>
<select id="test" name="test">
    <option value=""></option>
    <option value="a">a</option>
    <option value="b">b</option>
</select>
<p class="showme">This is my text</p>    
</li>
</ul>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小姐丶请自重 2024-08-25 03:13:16

我认为您处理问题/标记的方法是错误的。尝试对结构进行一些分段,这样就不会为父级设置动画。例如:

HTML:

<select id="selSomething">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<div class="tooltip">More information!</div>

Javascript:

 $(document).ready(function() {
      $('#selSomething').focus(function() {
          var $tip = $(this).next('.tooltip');
          if($tip.length > 0) {
            $tip.slideDown();
          }
      });
      $('#selSomething').blur(function() {
          var $tip = $(this).next('.tooltip');
          if($tip.length > 0) {
            $tip.slideUp();
          }
      })
  });

记住,简单就是完美:)

I think you're approaching the problem / markup wrong. Try segmenting the structure a bit so you're not animating the parent. For instance:

HTML:

<select id="selSomething">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<div class="tooltip">More information!</div>

Javascript:

 $(document).ready(function() {
      $('#selSomething').focus(function() {
          var $tip = $(this).next('.tooltip');
          if($tip.length > 0) {
            $tip.slideDown();
          }
      });
      $('#selSomething').blur(function() {
          var $tip = $(this).next('.tooltip');
          if($tip.length > 0) {
            $tip.slideUp();
          }
      })
  });

Remember, simplicity is perfection :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文