点击后不要滚动到顶部

时间:2021-02-01 18:51:19

标签: javascript css scroll

我真的被困住了,虽然我找到了一些提示和技巧 (javascript),但我不明白它的工作原理。问题是我有一个漂亮且功能强大的折叠菜单,但是当我点击主链接(荷兰、比利时、弗兰克里克、佐肯)时,页面会滚动到顶部。

我宁愿不使用 javascript onclick 解决这个问题,因为出于 SEO 的目的,我想保持“href”链接完整。

希望你们能帮帮我,不胜感激!

    <ul class="mainMenuA">
        <li class="item" id="nl">
            <a href="#nl" class="btnA"><span class="flag flag-nl"></span> Nederland</a>
            <div class="subMenu">
                <a href="/amsterdam">Amsterdam</a>
                <a href="/arnhem">Arnhem</a>
                <a href="/utrecht">Utrecht</a>
            </div>
        </li>
        <li class="item" id="be">
            <a href="#be" class="btnA"><span class="flag flag-be"></span> België</a>
            <div class="subMenu">
                <a href="/item1">item-1</a>
                <a href="/item2">item-2</a>
            </div>
        </li>
        <li class="item" id="fr">
            <a href="#fr" class="btnA"><span class="flag flag-fr"></span> Frankrijk</a>
            <div class="subMenu">
                <a href="/item1">item-1</a>
            </div>
        </li>
        <li class="item">
            <a href="#search" class="btnA"><i class="fas fa-search"></i>Zoeken</a>
        </li>
    </ul>



<style>

    a:hover {
    color:white;
    font-weight:bold;
    }

.mainMenuA{
    width: 250px;
    display: block;
    border-radius: 10px;
    overflow: hidden;
    list-style: none;
    font-weight:normal;
}
.item{
    border-top: 1px solid #ef584a;
    overflow: hidden;
}
.btnA{
    display: block;
    padding: 15px 20px;
    background-color: #ff4a52;
    color: #fff;
    position: relative;

}
.btnA:before{
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border-left: 8px solid transparent;
    border-right:8px solid transparent;
    border-top:10px solid #ff4a52;
    right: 15px;
    bottom: -10px;
    z-index: 9;
    
}

.btnA i {
    margin-right: 10px;
}
.subMenu{
    background: #273057;
    overflow: hidden;
    transition: max-height 0.7s;
    max-height: 0;
}
.subMenu a{
    display: block;
    padding: 15px 20px;
    color: #fff;
    font-size: 14px;
    border-bottom: 1px solid #394c7f;
    position: relative;
    
}
.subMenu a:before{
    content: '';
    opacity: 0;
    transition: opacity 0.3s;

}
.subMenu a:hover:before{
    content: '';
    position: absolute;
    height: 0;
    width: 6px;
    left: 0;
    top:0;
    opacity: 1;
    /* background-color: #d8d824; */
    border-top: 24px solid transparent;
    border-left: 11px solid #ffffff;
    border-bottom: 24px solid transparent;
}
.subMenu a:after{
    content: '';
    opacity: 0;
    transition: opacity 0.3s;

}
.subMenu a:hover:after{
    content: '';
    position: absolute;
    height: 0;
    width: 6px;
    right: 0px;
    top:0;
    opacity: 1;
    /* background-color: #d8d824; */
    border-top: 24px solid transparent;
    border-right: 11px solid #ffffff;
    border-bottom: 24px solid transparent;
}
.subMenu a:hover{
    background: #273057;
    background: -moz-linear-gradient(top, #273057 0%, #273057 50%, #394c7f 51%, #394c7f 100%);
    background: -webkit-linear-gradient(top, #273057 0%,#273057 50%,#394c7f 51%,#394c7f 100%);
    background: linear-gradient(to bottom, #273057 0%,#273057 50%,#394c7f 51%,#394c7f 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#273057', endColorstr='#394c7f',GradientType=0 );
    transition: all 0.3s;
    border-bottom: 1px solid #394c7f;
}
.subMenu a:last-child{
    border:none;
}
.item:target .subMenu{
    max-height: 1000em;
}

</style>

1 个答案:

答案 0 :(得分:0)

感谢您的快速响应。抱歉,我不清楚,我将它与 jquery、fontawesome 和 bootstrap 4 一起使用。目标是在您单击 # 行为时禁用浏览器功能,但不使用 onclick 功能进行 SEO。

此外,在这个主题中,这正是我所需要的,但我无法让它在我的情况下工作......How do I stop a web page from scrolling to the top when a link is clicked that triggers JavaScript?

相关问题