How to add display:inline-block in a jQuery show() function?

后端 未结 12 1951
执念已碎
执念已碎 2020-12-02 17:38

I have some code like this:

function switch_tabs(obj) {
    $(\'.tab-content\').hide();
    $(\'.tabs a\').removeClass(\"selected\");

    var id = obj.attr(         


        
相关标签:
12条回答
  • 2020-12-02 18:17

    actually jQuery simply clears the value of the 'display' property, and doesn't set it to 'block' (see internal implementation of jQuery.showHide()) -

       function showHide(elements, show) {
        var display, elem, hidden,
    

    ...

             if (show) {
                // Reset the inline display of this element to learn if it is
                // being hidden by cascaded rules or not
                if (!values[index] && display === "none") {
                    elem.style.display = "";
                }
    

    ...

            if (!show || elem.style.display === "none" || elem.style.display === "") {
                elem.style.display = show ? values[index] || "" : "none";
            }
        }
    

    Please note that you can override $.fn.show()/$.fn.hide(); storing original display in element itself when hiding (e.g. as an attribute or in the $.data()); and then applying it back again when showing.

    Also, using css important! will probably not work here - since setting a style inline is usually stronger than any other rule

    0 讨论(0)
  • 2020-12-02 18:18

    You can use animate insted of show/hide

    Something like this:

    function switch_tabs(obj)
    {
        $('.tab-content').animate({opacity:0},3000);
        $('.tabs a').removeClass("selected");
        var id = obj.attr("rel");
    
        $('#'+id).animate({opacity:1},3000);
        obj.addClass("selected");
    }
    
    0 讨论(0)
  • 2020-12-02 18:18

    The best .let it's parent display :inline-block or add a parent div what CSS only have display :inline-block.

    0 讨论(0)
  • 2020-12-02 18:19

    Instead of show, try to use CSS to hide and show the content.

    function switch_tabs(obj) {
        $('.tab-content').css('display', 'none'); // you could still use `.hide()` here
        $('.tabs a').removeClass("selected");
        var id = obj.attr("rel");
    
        $('#' + id).css('display', 'inline-block');
        obj.addClass("selected");
    }
    
    0 讨论(0)
  • 2020-12-02 18:25
    <style>
    .demo-ele{display:inline-block}
    </style>
    
    <div class="demo-ele" style="display:none">...</div>
    
    <script>
    $(".demo-ele").show(1000);//hide first, show with inline-block
    <script>
    
    0 讨论(0)
  • 2020-12-02 18:30

    I did that

    function showPanels()  {
        $('.panels').show("slow");
        $('.panels').css('display','inline-block');
    }
    

    works like a charm.

    0 讨论(0)
提交回复
热议问题