【问题标题】:.animate({ width: 'toggle' }) without messing up the content.animate({ width: 'toggle' }) 不会弄乱内容
【发布时间】:2013-05-20 12:01:45
【问题描述】:

所以我的左滑 div 有问题。目前我正在使用$('#searchBox').animate({ width: 'toggle' }); 来显示它,但是当它滑动时,它看起来里面的内容被忽略了它们的浮动。它们在 div 停止滑动后固定,但在滑动时会产生奇怪的效果。我正在使用的代码:

// Search bar dropdown script

    function searchBar() {
        $('#searchBox').animate({ width: 'toggle' });
        $('#searchBoxButton').fadeToggle('fast');
    }

    function searchBarClose() {
        $('#searchBox').animate({ width: 'toggle' });
        $('#searchBoxButton').fadeToggle('fast');
    }

<div id="searchBoxButton" onclick="searchBar()"></div>
<div id="searchBox">
    <form>
        <div id="magnifier"></div>
        <input id="searchBoxInput" placeholder="Search me" type="text"/>
        <div class="closeButton" onclick="searchBarClose()"></div>
    </form>
</div>

    #searchBoxButton {
    width: 50px;
    height: 50px;
    background-color: #000;
    right: 0;
    position: absolute;
    margin: -5px auto 0 auto;
}

#searchBox {
    display: none;
    right: 0;
    position: fixed;
    margin: -5px auto 0 auto;
    background-color: #202020;
    z-index: 1;
    padding: 3px 0;
    border: 1px solid #151515;
}

#searchBox input[type="text"] {
    width: 150px;
    outline: none;
    padding: 3px 2px;
    background-color: #3F3F3F;
    border: 1px solid #4d4d4d;
    border-radius: 3px;
    font-family: "Trebuchet MS" sans-serif;
    color: #c0c0c0;
    float:left;
}

#searchBox input[type="text"]:focus {
    border: 1px solid #797979;
    box-shadow: 0px 0px 15px -2px #797979;
    background-color: #444444;
}

#searchBoxInput::-webkit-input-placeholder { /* WebKit browsers */
    color:    #7d7d7d;
}
#searchBoxInput:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
    color:    #7d7d7d;
}
#searchBoxInput::-moz-placeholder { /* Mozilla Firefox 19+ */
    color:    #7d7d7d;
}
#searchBoxInput:-ms-input-placeholder { /* Internet Explorer 10+ */
    color:    #7d7d7d;
}

#magnifier {
    background: url("http://placehold.it/13x13") no-repeat;
    width: 13px; height: 13px;
    float: left;
    margin: 7px 6px;
}

.closeButton {
    border-radius: 15px;
    cursor: pointer;
    background: url("http://placehold.it/15x15") center no-repeat;
    width: 15px; height: 15px;
    float: left;
    -webkit-transition: background 200ms ease-in-out;
    -moz-transition: background 200ms ease-in-out;
    -o-transition: background 200ms ease-in-out;
    transition: background 200ms ease-in-out;
}

#searchBox .closeButton {
    margin: 7px 4px;
    float: left;
}

.closeButton:hover {
    background-color: #373737;
    -webkit-transition: background 200ms ease-in-out;
    -moz-transition: background 200ms ease-in-out;
    -o-transition: background 200ms ease-in-out;
    transition: background 200ms ease-in-out;
}

.closeButton:active {
    background-color: #212121;
}

我知道解决此问题的方法是在#magnifier#searchBoxInput.closeButton 上使用绝对位置,但这对我不利。有没有其他方法可以做到这一点?

小提琴:http://tinker.io/ef4f7

【问题讨论】:

    标签: jquery


    【解决方案1】:

    由于宽度,它的行为如此。它没有足够的宽度来显示浮动在同一行上的项目。没有足够的宽度来显示它们。相反,您可以切换正确的位置。

    Demo

     function searchBar() {
            $('#searchBox').animate({ right: 'toggle' });
            $('#searchBoxButton').fadeToggle('fast');
        }
    
        function searchBarClose() {
            $('#searchBox').animate({ right: 'toggle' });
            $('#searchBoxButton').fadeToggle('fast');
        }
    

    带幻灯片效果

    Demo

    function searchBar() {
          $('#searchBox').show('slide', {
              direction: 'right'
          }, 2000);
          $('#searchBoxButton').fadeToggle('slow');
      }
    
      function searchBarClose() {
          $('#searchBox').hide('slide', {
              direction: 'right'
          }, 2000, function () {
              $('#searchBoxButton').fadeToggle('slow');
          });
    
      }
    

    【讨论】:

    • 没有办法让它像旧的那样滑动吗?这样它似乎只是出现没有任何效果。不过,感谢您的回答。
    • @Gus 使用工作 jsfiddle 在下面查看我的答案。我认为这是您想要的。
    • 是的,您可以使用 jquery ui 效果来制作幻灯片效果。
    【解决方案2】:

    这是一个 jsfiddle,其功能更像您想要使用宽度而不是右。请注意,我更改了您的一些 CSS 并排除了搜索按钮本身,因为我无权访问该图像。但简而言之,这是主要的解决方法:

    function searchBar() {
            $('input').fadeIn('fast', function () {
              $('#searchBox').animate({ width: 'toggle' });
          });
    
    }
    

    看到您需要输入框宽度,这样您就不会遇到尺寸问题。以上应该可以工作,这里是jsfiddle

    注意:这里有一个额外的锚点,只是为了再次演示,因为我无法访问您的放大镜按钮,但您应该明白这一点。如果它没有按您的意愿运行,请告诉我。

    【讨论】:

    • 感谢您的回答!我在小提琴上使用了占位符图像,但忘记在代码上更新它,已经编辑了帖子。所以基本的想法是隐藏输入、放大镜和 closeButton 并在调用 searchBar() 函数时显示它们?我在适应它时遇到了一些麻烦,但我想我已经掌握了基本概念。
    • @Gus 如果正确请标记为正确。如果您需要进一步的帮助,请使用正确的引用更新小提琴,我会为您修复它。
    • tinker.io/ef4f7 这个有占位符图像,应该可以正常工作。 searchBoxButton 应该是黑色的。
    • 没关系,PSL 的回答解决了我的问题。不过,我真的很感谢您抽出时间来帮助我。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-17
    • 2021-12-29
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多