gpt4 book ai didi

javascript - 在固定位置 div 上使用 jquery 淡入/淡出功能时防止页面自动向上滚动

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:28 27 4
gpt4 key购买 nike

我有一个网页,我想通过点击一个按钮来制作一个出现在屏幕右侧的div(div和按钮都有固定的位置)我使用 jQuery fadein/out 使它出现

它工作正常但是当我点击按钮时,页面会自动滑到顶部

html代码:

<div class="fixedDiv">
<div class="fixedDiv liveSupportFrame" style="background: #f8aa00;display: none" id="lsf">
<div class="container" style="background-color:#d8d6d6;">
............
</div>
</div>

<a class="btn btn-warning sideBtn" onclick="showOrHideLiveSupport();" role="button" href="#">FAQ</a>

CSS 代码:

.fixedDiv{
position: fixed;
bottom: 25%;
right: 0em;
}
.fixedDiv .liveSupportFrame{
margin-right: 2em;
margin-bottom: 1em;
font-size:1.6vw;
bottom: 30%;
}

JS代码:

        var isLsfOpen = 0;
function showOrHideLiveSupport() {
if(!isLsfOpen){
showLiveSupport()
}
else{
hideLiveSupport()
}
}
function showLiveSupport() {
$('#lsf').fadeIn();

isLsfOpen=true;
}
function hideLiveSupport() {
$('#lsf').fadeOut();
isLsfOpen=false;
}

最佳答案

那是因为 href="#"。在您的处理程序中使用 event.preventDefault();

关于javascript - 在固定位置 div 上使用 jquery 淡入/淡出功能时防止页面自动向上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47663727/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com