Jquery show()在表演时破坏CSS位置属性

jquery show() breaks css position properties while acting

本文关键字:CSS 位置 属性 表演 show Jquery      更新时间:2023-09-26

我的问题是在我的网站上,当我加载页面并单击主页时,如果文章在屏幕上可见,我编写的js将其平滑地隐藏起来,没有问题。但是当我点击About Us链接时,它显示了文章,但是在它的动作完成时,css位置被破坏了。

下面是代码: HTML:

<nav>
    <ul class="sf-menu sf-vertical">
        <li><a href=# onclick=home()>Home</a></li>
        <li><a href=#about onclick=about()>About Us</a></li>
        <li><a href=#>Cuisines</a>
            <ul>
                <li><a href=#starters>Starters</a></li>
                <li><a href=#>Main Dishes</a></li>
                <li><a href=#>Desserts</a></li>
                <li><a href=#>Mezes</a></li>
            </ul>
        </li>
        <li><a href=#>Wine List</a></li>
        <li><a href=#gallery>Gallery</a></li>
        <li><a href=#contacts>Contacts</a></li>
    </ul>
</nav>
<article class=vanished id=about>
    <h1>About Us</h1>
    <div class="main-wrapper scroll">
        <div class="wrapper clearfix">
            <img src=img/bazar-place.png alt=bazar-place width=222 height=150 class=about-img>
            <h4>Our Restaurant</h4>
            <p>Bazar is a restaurant located between the districts Haga and Vasastaden in Gothenburg with a focus on Turkish and Persian food of the best quality that creates opportunities for an exciting mix all the way, from appetizer to dessert.</p>
        </div>
        <div class="wrapper clearfix">
            <img src=img/belly.jpg alt=belly-dancing width=222 height=167 class=about-img id=belly>
            <h4>Events</h4>
            <p>Every Saturday from 21, we have Belly dancing at Bazar.</p>
        </div>
        <div class="wrapper clearfix">
            <img src=img/food.jpg alt=food-services width=222 height=167 class=about-img id=food>
            <h4>Food Services</h4>
            <p>Taste our famous pasta dish to the human price of 69 :- We offer 10% discount for take-away at our entire menu.</p>
            <p>At lunchtime you can choose from three Turkish pasta dishes or among two or three different main dishes. Ask about vegetarian options! Drinking, salad and coffee / tea included. Lunch can also be picked up.</p>
        </div>
        <div class="wrapper last clearfix">
            <img src=img/extra.jpg alt=extra-services width=222 height=167 class=about-img id=extra>
            <h4>Extra Services</h4>
            <p>We can help with everything from after work, kick-off to the birthday called with food and drink that lasts all night. Do you want more entertainment we can arrange live music and belly dancing.</p>
        </div>
    </div>
</article>
JAVASCRIPT:

function home(){
    if ($(".active") == [])
    {
        return ;
    }
    else
    {
        var active = $(".active");
        active.css("display","inline-block");
        active.hide("slide",{direction:"left"},700);
        active.attr("class","vanished");
    }
}
function about(){
    if ($(".active") == [])
    {
        var hidden = $("#about");
        hidden.css("display","inline-block");
        hidden.show("slide",{direction : "right"},700);
        hidden.attr("class","active");
    }
    else
    {
        if ($("#about").attr("class") == "active")
        {
            return ;
        }
        else
        {
            var active = $(".active");
            active.css("display","inline-block");
            active.hide("slide",{direction:"left"},700);
            active.attr("class","vanished");
            var hidden = $("#about");
            hidden.css("display","inline-block");
            hidden.show("slide",{direction : "right"},700);
            hidden.attr("class","active");
        }
    }
}
CSS:

article{
    position: absolute;
    width: 550px;
    height: 100%;
    background-image: url("../img/blockBG.png");
    z-index: 1;
    left: 235px;
    border-left: 1px solid #4A4A4A;
    border-right-color: #7b7b7b;
    border-right-style: solid;
    border-right-width: thick;
    padding-right: 40px;
    padding-left: 40px;
    text-align: center;
}
.vanished{
    display:none;
}
.main-wrapper{
    position:relative;
    z-index:1;
    width: 100%;
    height: 590px;
    display:block;
}
.wrapper{
    border-bottom:1px solid #4A4A4A;
    margin-bottom: 15px;
}
.last{
    border: none;
}
h4{
    color: #efefef;
    text-decoration: none;
    text-align: left;
    font-family:'Yeseva One',cursive;
    font-size: 17px;
    font-weight: normal;
    margin-bottom: 10px;
    margin-top: 0;
    line-height: 19px;
}
h1{
    position: relative;
    font-family:'Yeseva One',cursive;
    font-size:60px;
    z-index: 2;
    color: white;
    margin-top: 90px;
    padding-bottom: 41px;
    margin-bottom: 20px;
    font-weight: normal;
    border-bottom:1px solid #4A4A4A;
}
p{
    color: #efefef;
    text-decoration: none;
    text-align: left;
    font-family:Arial, sans-serif;
    font-size: 12px;
}
.clearfix:before,
.clearfix:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}
.clearfix:after {
    clear: both;
}
/*
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.clearfix {
    *zoom: 1;
}

问题是<h1> margin-top只在动画结束时正确应用。

尝试将距离顶部改为内边距而不是边距,它应该修复它:

h1{
    position: relative;
    font-family:'Yeseva One',cursive;
    font-size:60px;
    z-index: 2;
    color: white;
    margin-top: 0;
    padding-top: 90px;
    padding-bottom: 41px;
    margin-bottom: 20px;
    font-weight: normal;
    border-bottom:1px solid #4A4A4A;
}

或者,您可以给父<article>填充或边框顶部。这将防止<h1>边距在动画运行时在容器外坍缩。