【问题标题】:Shadowbox doesn't load current URLShadowbox 不加载当前 URL
【发布时间】:2013-09-15 23:43:56
【问题描述】:

我正在使用一个集成了 shadowbox 的简单图片库。 这是正常的脚本。

 <div id="gallery">
            <div id="panel">
            <p><a id="showimg" href="images/image_01.jpg" rel="shadowbox">
                <img id="largeImage" src="images/image_01.jpg" />
                </a></p>
            <div id="description">1st image description</div>
            </div>

            <div id="thumbs">
                <img src="images/image_01.jpg" alt="1st image description" />
                <img src="images/image_02.jpg" alt="2nd image description" />
                <img src="images/image_03.jpg" alt="3rd image description" />
                <img src="images/image_04.jpg" alt="4th image description" />
                <img src="images/image_05.jpg" alt="5th image description" />
            </div>
        </div>

        </div>

        <script>

        $('#thumbs').delegate('img','click', function(){
            $('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
            $('#description').html($(this).attr('alt'));
        });

        </script>

我注意到大图像中的 url 从未改变,因此我在脚本中添加了另一行以在鼠标更改图像时更改 href。

<script>

$('#thumbs').delegate('img','click', function(){
    $('#largeImage').attr('src',$(this).attr('src'));
    $('#description').html($(this).attr('alt'));
    $('#showimg').attr('href', $(this).attr('src'));
});

</script>

我不知道它是否正确,但是当我通过鼠标悬停图像时,URL 现在总是不同,但 SHADOWBOX 总是加载相同的图像(第一个)。

请帮忙!

【问题讨论】:

    标签: javascript html image gallery shadowbox


    【解决方案1】:

    尝试使用这种语法:

    html(给列表中的每个图像添加类):

    <div id="thumbs">
    <img class="imglist" src="images/image_01_thumb.jpg" alt="1st image description" />
    </div>
    

    js:

    $('.imglist').on('click','img', function(e){
    
    e.stopPropagation(); or e.preventDefault();
    
           $('#largeImage').attr('src',$(this).attr('src').replace('thumb','large'));
                $('#description').html($(this).attr('alt'));
                $('#showimg').attr('href', $(this).attr('src').replace('thumb', 'large'));
    
             });
    

    【讨论】:

    • “尝试编写此方法 e.stopPropagation() 或 e.preventDefault()”是什么意思?我从未编写过 Javascript 代码:/
    • 先尝试,如果没有任何变化,然后再尝试
    • 抱歉,什么也没发生。大图甚至没有改变
    • 这个 $(this).attr('src').replace('thumb','large')) 是什么意思?
    • 我已经更改了...忘记替换...它只会替换我不再使用的文本。它将把#thumbs 中的src 归于#largeimage 中的src。而且我必须使用#thumbs 而不是.imglist,因为该函数无法获取img 标签!
    猜你喜欢
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2014-10-03
    相关资源
    最近更新 更多