0

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

我在 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> 

任何人都可以提供建议吗?

谢谢

4

1 回答 1

3

使用.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');
        }
    });
于 2012-07-18T14:40:39.973 回答