【问题标题】:Area map into a bootstrap tab not working区域映射到引导选项卡不起作用
【发布时间】:2018-10-29 21:38:55
【问题描述】:

我有一个运行良好的引导选项卡。 如果未将其插入选项卡,我的区域图运行良好。 我使用来自 Matt Stow 的 Responsive Image Maps jQuery 插件,也可以正常工作。

症状: 然后我将区域图放入其中一个选项卡中,默认情况下是不活动的。 然后我单击选项卡使其显示。所以 img 显示得很好。 但是区域地图不起作用。我看不到可点击的矩形。 但是,如果我手动调整导航器的大小,那么区域地图就会起作用。

页面:https://boutique.bilp.fr/71-les-pieds-de-poteaux.html 选择选项卡“Guide de choix”,白色矩形应该是可点击的。直到我手动调整窗口大小,它们才会出现。

原因: 负责的是 Responsive Image Maps jQuery Plugin。在其代码中,它调用 jquery .width() 方法来获取地图应该工作的 img 的宽度。并且因为父级(tab)被隐藏了,所以返回的宽度是错误的。它使用它来调整地图的大小......使用错误的值。地图是如此之小,以至于它似乎无法工作。

感谢您的帮助。

【问题讨论】:

    标签: twitter-bootstrap-3 jquery-plugins tabs responsive area


    【解决方案1】:

    一种解决方案是通过在调用 width() 之前使祖先可见来修改响应式图像映射 jQuery 插件。

    原码:

    /*
    * rwdImageMaps jQuery plugin v1.6
    *
    * Allows image maps to be used in a responsive design by     recalculating the area coordinates to match the actual image size on load and window.resize
    *
    * Copyright (c) 2016 Matt Stow
    * https://github.com/stowball/jQuery-rwdImageMaps
    * http://mattstow.com
    * Licensed under the MIT license
    */
    ;(function($) {
    $.fn.rwdImageMaps = function() {
        var $img = this;
    
        var rwdImageMap = function() {
            $img.each(function() {
                if (typeof($(this).attr('usemap')) == 'undefined')
                    return;
    
                var that = this,
                    $that = $(that);
    
                // Since WebKit doesn't know the height until after the image has loaded, perform everything in an onload copy
                $('<img />').on('load', function() {
                    var attrW = 'width',
                        attrH = 'height',
                        w = $that.attr(attrW),
                        h = $that.attr(attrH);
    
                    if (!w || !h) {
                        var temp = new Image();
                        temp.src = $that.attr('src');
                        if (!w)
                            w = temp.width;
                        if (!h)
                            h = temp.height;
                    }
    
                    var wPercent = $that.width()/100,
                        hPercent = $that.height()/100,
                        map = $that.attr('usemap').replace('#', ''),
                        c = 'coords';
    
                    $('map[name="' + map + '"]').find('area').each(function() {
                        var $this = $(this);
                        if (!$this.data(c))
                            $this.data(c, $this.attr(c));
    
                        var coords = $this.data(c).split(','),
                            coordsPercent = new Array(coords.length);
    
                        for (var i = 0; i < coordsPercent.length; ++i) {
                            if (i % 2 === 0)
                                coordsPercent[i] = parseInt(((coords[i]/w)*100)*wPercent);
                            else
                                coordsPercent[i] = parseInt(((coords[i]/h)*100)*hPercent);
                        }
                        $this.attr(c, coordsPercent.toString());
                    });
                }).attr('src', $that.attr('src'));
            });
        };
        $(window).resize(rwdImageMap).trigger('resize');
    
        return this;
    };
    })(jQuery);
    

    修改后的代码:

    /*
    * rwdImageMaps jQuery plugin v1.6
    *
    * Allows image maps to be used in a responsive design by recalculating the area coordinates to match the actual image size on load and window.resize
    *
    * Copyright (c) 2016 Matt Stow
    * https://github.com/stowball/jQuery-rwdImageMaps
    * http://mattstow.com
    * Licensed under the MIT license
    */
    ;(function($) {
    $.fn.rwdImageMaps = function() {
        var $img = this;
    
        var rwdImageMap = function() {
    
            $img.each(function() {
                if (typeof($(this).attr('usemap')) == 'undefined')
                    return;
    
                var that = this,
                    $that = $(that);
    
                    // Since WebKit doesn't know the height until after the image has loaded, perform everything in an onload copy
                $('<img />').on('load', function() {
    
                    // Modif BC : make ancestors visible so .width() can return the right value
                    //************************************************
                    var hidden_ancestors = [];
                    $that.parents().each(function() {
                        if ($(this).css('display') == 'none')
                        {
    
                    $(this).show();
                            hidden_ancestors.push($(this));
                        };
                    });
                    // END Modif BC
    
                    var attrW = 'width',
                        attrH = 'height',
                        w = $that.attr(attrW),
                        h = $that.attr(attrH);
    
                    if (!w || !h) {
                        var temp = new Image();
                        temp.src = $that.attr('src');
                        if (!w)
                            w = temp.width;
                        if (!h)
                            h = temp.height;
                    }
    
                    var wPercent = $that.width()/100,
                        hPercent = $that.height()/100,
                        map = $that.attr('usemap').replace('#', ''),
                        c = 'coords';
    
                    $('map[name="' + map + '"]').find('area').each(function() {
                        var $this = $(this);
                        if (!$this.data(c))
                            $this.data(c, $this.attr(c));
    
                        var coords = $this.data(c).split(','),
                            coordsPercent = new Array(coords.length);
    
                        for (var i = 0; i < coordsPercent.length; ++i) {
                            if (i % 2 === 0)
                                coordsPercent[i] = parseInt(((coords[i]/w)*100)*wPercent);
                            else
                                coordsPercent[i] = parseInt(((coords[i]/h)*100)*hPercent);
                        }
                        $this.attr(c, coordsPercent.toString());
                    });
    
    
                    // Modif BC : Restore invisibility on ancestors
                    //*********************************************
                    jQuery.each(hidden_ancestors, function(index, value)
                    {
                        $(value).css({display: ''});
                    });
                    // END Modif BC
    
                }).attr('src', $that.attr('src'));
            });
        };
        $(window).resize(rwdImageMap).trigger('resize');
    
        return this;
    };
    })(jQuery);
    

    我将向作者 Matt Stow 提出这项改进

    【讨论】:

    • 像魅力一样工作!非常感谢布鲁诺!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2018-05-16
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2013-05-27
    相关资源
    最近更新 更多