【问题标题】:How to show/hide big image by clicking on thumbnails?如何通过单击缩略图来显示/隐藏大图像?
【发布时间】:2023-04-01 22:47:02
【问题描述】:

如何通过点击缩略图来显示/隐藏大图?

我需要这样的

在这里试试 JSFiddle http://jsfiddle.net/jitendravyas/Qhdaz/

是否可以仅使用 CSS。如果没有,那么 jQuery 解决方案就可以了。

使用<a href=#"> 是否很好,即使它没有在同一标签或新标签中打开任何新页面。

编辑:

我忘记添加了。它应该也可以在 iPad 上运行

【问题讨论】:

    标签: javascript jquery css mobile-safari


    【解决方案1】:

    看这个例子:

    无预加载

    HTML:

    <div id="big-image">
        <img src="http://lorempixel.com/400/200/sports/1/">
    </div>
    
    <div class="small-images">
        <a href="http://lorempixel.com/400/200/sports/1/"><img src="http://lorempixel.com/100/50/sports/1/"></a>
    <a href="http://lorempixel.com/400/200/fashion/1/" class=""><img src="http://lorempixel.com/100/50/fashion/1/"></a>
    <a href="http://lorempixel.com/400/200/city/1/"><img src="http://lorempixel.com/100/50/city/1/"></a>
    </div>
    

    Javascript (jQuery)

    $(function(){
        $(".small-images a").click(function(e){
            var href = $(this).attr("href");
            $("#big-image img").attr("src", href);
            e.preventDefault();
            return false;
        });
    });
    

    目前只有1张大图,点击A时,A的href会被复制为大图的SRC。

    现场示例:http://jsfiddle.net/Qhdaz/1/

    如果你不想在没有额外 DOM 的情况下这样做,你可以添加 3 个大图像,然后直接加载它们。上面的解决方案没有预加载图片,下面的函数会。

    预加载

    HTML:

    <div id="big-image">
        <img src="http://lorempixel.com/400/200/sports/1/">
        <img src="http://lorempixel.com/400/200/fashion/1/">
        <img src="http://lorempixel.com/400/200/city/1/">
    </div>
    
    <div class="small-images">
        <img src="http://lorempixel.com/100/50/sports/1/">
        <img src="http://lorempixel.com/100/50/fashion/1/">
        <img src="http://lorempixel.com/100/50/city/1/">
    </div>
    

    Javascript:

    $(function(){
        $("#big-image img:eq(0)").nextAll().hide();
        $(".small-images img").click(function(e){
            var index = $(this).index();
            $("#big-image img").eq(index).show().siblings().hide();
        });
    });
    

    http://jsfiddle.net/Qhdaz/2/

    【讨论】:

    • 这会为 img 多次添加更多的 dom 计算。不是效率。
    • 我已经用另一个功能更新了它。第一个函数不预加载图像,第二个函数直接加载。 @steven.yang 它必须重建,但在普通 PC 上这应该没有任何问题,但在 iPad 上它可以,因为处理和内存是有限的。
    • @Niels - 感谢您的回答。你也可以试试这个问题stackoverflow.com/questions/8098107/…
    • 目前没有足够的时间来做,如果你还没有任何回应,我今晚试试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多