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

标签 jquery html css gallery

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

我用 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/

相关文章:

jquery - 在运行 javascript 代码之前延迟

javascript - SetInterval 只调用一次,不知道为什么?

javascript - jquery: js-cookie 在 ie7 中不工作?

javascript - 使用关键 CSS 时,Jquery 函数不会在加载时运行

css - 设置样式的问题

html - 为什么我的 div 框在我向左设置 float 时向下堆叠?

javascript - IFrame 中不会发生事件冒泡

html - Bootstrap 按钮文本未调整大小

css - 在固定页眉/页脚的移动页面上滚动内容

javascript - 浏览器加载同一张图片 2 次