【问题标题】:Blueimp Gallery : External link on carousel imagesBlueimp Gallery : 轮播图像上的外部链接
【发布时间】:2016-09-22 22:24:36
【问题描述】:

我已经使用 Blueimp Gallery 插件 (https://github.com/blueimp/Gallery) 设置了一个轮播,它可以正常工作,但是当我单击图像时,我想禁用控件切换,而是让默认链接行为指向网站的另一个页面.

我需要指标始终可见。 是否可以使用“a”标签的 href 值转到另一个页面?

HTML:

<div id="images-slider">
    <a href="http://www.google.fr" data-link="http://lorempixel.com/878/330/nature/9/">
        <img src="http://lorempixel.com/878/330/nature/9/" alt="">
    </a>
    <a href="http://www.google.fr" data-link="http://lorempixel.com/878/330/nature/4/">
        <img src="http://lorempixel.com/878/330/nature/4/" alt="">
    </a>
    <a href="http://www.google.fr" data-link="http://lorempixel.com/878/330/nature/5/">
        <img src="http://lorempixel.com/878/330/nature/5/" alt="">
    </a> 
</div>

JS:

blueimp.Gallery(
    document.getElementById('images-slider').getElementsByTagName('a'),
    {
        container: '#slider',
        carousel: true,
        thumbnailIndicators: false,
        urlProperty: 'link'
    }
);

这是一个小提琴:http://jsfiddle.net/0hb7gdeq/1/

谢谢

【问题讨论】:

    标签: javascript carousel blueimp


    【解决方案1】:

    我认为这在任何方面都不是最佳的,但我通过像这样在 innerHTML 周围包裹另一个标签来让它工作。

    blueimp.Gallery(
        document.getElementById('images-slider').getElementsByTagName('a'),
        {
        container: '#slider',
        carousel: true,
        thumbnailIndicators: false,
        urlProperty: 'link',
        onslidecomplete: function (index, slide) {
            var href = this.list[index].getAttribute('href');
            slide.innerHTML = "<a href='"+href+"'>" + slide.innerHTML + "</a>";
        }
    );
    

    在这次 hack 之后,css 也需要调整

    .blueimp-gallery>.slides .slide .slide-content {
        position: relative;
        float: left;
        height: 100%;
        text-align: center;
    }
    

    【讨论】:

    • 谢谢你的解决方案,伙计!
    【解决方案2】:

    感谢 oBo,我扩展了您的代码,使其也可以使用我在幻灯片上需要的 title 属性,并使用一些 jQuery 来查找 title 属性。

    var title;
    // [...]
    
    onslidecomplete: function(index, slide) {
                            title = $(slide).find("img").attr("title");
                            var href = this.list[index].getAttribute('href');
                            slide.innerHTML = "<a href='" + href + "' title='"+title+"'>" + slide.innerHTML + "</a>";
                        }
    

    完整代码:

    HTML

    <!-- The Gallery as lightbox dialog, should be a child element of the document body -->
            <div id="blueimp-gallery-carousel-home" class="my-blueimp-gallery blueimp-gallery blueimp-gallery-carousel blueimp-gallery-controls">
                <div class="slides"></div>
                <div class="menu-tile-text">
                    <h4 class="title"></h4>
                    <span class="icon-bar"></span>
                </div>
                <a class="play-pause"></a>
                <ol style="bottom: 12px;" class="indicator"></ol>
            </div>
    
            <div id="links-home" style="display:none;">
                <a href="#/somesite" data-link="assets/img/640x360.jpg" title="banana">
                    <img src="assets/img/640x360.jpg" alt="banana">
                </a>
                <a href="#/site" data-link="assets/img/640x360.jpg" title="banana">
                    <img src="assets/img/640x360.jpg" alt="banana">
                </a>
            </div>
    

    JS

    $(document).ready(function() {
    
            //BlueImp Carousel
            document.getElementById('links-home').onclick = function(event) {
                event = event || window.event;
                var target = event.target || event.srcElement,
                    link = target.src ? target.parentNode : target,
                    options = {
                        index: link,
                        event: event
                    },
                    links = this.getElementsByTagName('a');
                blueimp.Gallery(links, options);
            };
    
            var title;
            blueimp.Gallery(
                document.getElementById('links-home').getElementsByTagName('a'), {
                    container: '#blueimp-gallery-carousel-home',
                    carousel: true,
                    thumbnailIndicators: false,
                    transitionSpeed: 400,
                    slideshowInterval: 8000,
                    clearSlides: true,
                    titleElement: 'h4',
                    urlProperty: 'link',
                    onslidecomplete: function(index, slide) {
                        title = $(slide).find("img").attr("title");
                        var href = this.list[index].getAttribute('href');
                        slide.innerHTML = "<a href='" + href + "' title='" + title + "'>" + slide.innerHTML + "</a>";
                    }
    
                }
            );
    
        });
    

    【讨论】:

    • 正如您几天前实施的那样,也许您可​​以帮助我,因为我无法使其工作。我有我的&lt;div id="links"&gt;,所以我设置了document.getElementById('links'),但是我不知道container: '#slider' 是什么,应该用blueimp-gallery-carousel 代替slider 吗?但即使这样,当我点击它时也不会打开图片......
    • 抱歉,我无法再访问我的实现,因为它是一种秘密工作项目。我建议您检查元素 (F12) 并查看图像 href 的去向。
    • 好的。那是我看不到它们添加的东西,但我会继续搜索。谢谢
    • 再次访问我的代码!我刚刚添加了完整的实现,希望对您有所帮助!
    • 太棒了!我本可以让你的榜样!我会努力实现我所需要的!谢谢!
    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多