单击图库中的链接会导致页面“跳转”?

时间:2012-07-18 14:39:26

标签: 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> 

有人可以提供建议吗?

由于

1 个答案:

答案 0 :(得分: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');
        }
    });