gpt4 book ai didi

jquery - 单击图库中的链接会导致页面转到 'jump'?

转载 作者:太空宇宙 更新时间:2023-11-04 00:15:18 25 4
gpt4 key购买 nike

请原谅标题措辞不佳,希望我能在这里解释这个问题。

我用 jQuery 编写了一个简单的画廊控件,其中有六个图像和两个按钮控件,允许用户循环浏览整个图像集合。我的这部分工作正常,但是每当我单击一个按钮在集合中移动时,页面会自动“跳转/滚动”到页面顶部吗?我试过给画廊容器一个固定的高度,因为我有一个类似的问题,在此之前我设法解决了这个问题,但这没有帮助:

这是 jQuery:

var index = 0;
$(function () {
$('#btnLeft').click(function () {
if (index != 0) {
index--;
$('#sixth').attr('src', $('#fifth').attr('src'));
$('#fifth').attr('src', $('#fourth').attr('src'));
$('#fourth').attr('src', $('#third').attr('src'));
$('#third').attr('src', $('#second').attr('src'));
$('#second').attr('src', $('#first').attr('src'));
$('#first').attr('src', '/Content/Images/Gallery/Thumbs/' + parseInt(index + 1) + '.png');
}
});

$('#btnRight').click(function () {
if (parseInt(6 + index) != 10) {
index++;
$('#first').attr('src', $('#second').attr('src'));
$('#second').attr('src', $('#third').attr('src'));
$('#third').attr('src', $('#fourth').attr('src'));
$('#fourth').attr('src', $('#fifth').attr('src'));
$('#fifth').attr('src', $('#sixth').attr('src'));
$('#sixth').attr('src', '/Content/Images/Gallery/Thumbs/' + parseInt(6 + index) + '.png');
}
});
});

这是标记:

<div id="gallerySlider" style="height: 160px;">
<img id="first" src="/Content/Images/Gallery/Thumbs/1.png" alt="Image" width="160" height="160" style="float:left;" />
<img id="second" src="/Content/Images/Gallery/Thumbs/2.png" alt="Image" width="160" height="160" style="float:left;" />
<img id="third" src="/Content/Images/Gallery/Thumbs/3.png" alt="Image" width="160" height="160" style="float:left;" />
<img id="fourth" src="/Content/Images/Gallery/Thumbs/4.png" alt="Image" width="160" height="160" style="float:left;" />
<img id="fifth" src="/Content/Images/Gallery/Thumbs/5.png" alt="Image" width="160" height="160" style="float:left;" />
<img id="sixth" src="/Content/Images/Gallery/Thumbs/6.png" alt="Image" width="160" height="160" style="float:left;" />

<a id="btnLeft" style="position:relative; float:left; bottom:105px;" href="#"><img src="/Content/Images/Design/leftbutton.png" alt="Left Button" /></a>
<a id="btnRight" href="#" style="position:relative; float:right; bottom:105px;"><img src="/Content/Images/Design/rightbutton.png" alt="Right Button" /></a>
</div>

有人可以提供建议吗?

谢谢

最佳答案

使用.preventDefault()像下面这样停止浏览器默认加载和跳转:

$('#btnLeft').click(function (e) {

e.preventDefault();

if (index != 0) {
index--;
$('#sixth').attr('src', $('#fifth').attr('src'));
$('#fifth').attr('src', $('#fourth').attr('src'));
$('#fourth').attr('src', $('#third').attr('src'));
$('#third').attr('src', $('#second').attr('src'));
$('#second').attr('src', $('#first').attr('src'));
$('#first').attr('src', '/Content/Images/Gallery/Thumbs/' + parseInt(index + 1) + '.png');
}
});

$('#btnRight').click(function (e) {

e.preventDefault();

if (parseInt(6 + index) != 10) {
index++;
$('#first').attr('src', $('#second').attr('src'));
$('#second').attr('src', $('#third').attr('src'));
$('#third').attr('src', $('#fourth').attr('src'));
$('#fourth').attr('src', $('#fifth').attr('src'));
$('#fifth').attr('src', $('#sixth').attr('src'));
$('#sixth').attr('src', '/Content/Images/Gallery/Thumbs/' + parseInt(6 + index) + '.png');
}
});

关于jquery - 单击图库中的链接会导致页面转到 'jump'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11543944/

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