【发布时间】:2015-10-23 15:07:23
【问题描述】:
我编写了一个脚本来为我网站上的大多数图片添加一个 Pinterest 按钮。问题是当某人在浏览器中启用Chrome Pin it extension 时,“固定”按钮会为用户显示两次。
无论如何,在 JavaScript 中是否有检查用户是否在他们的浏览器中启用了这个扩展?
(function($) {
$(function() {
$('.container img').each(function() {
if ($(this).parent('a')) {
var $permalink = $(this).parent('a').attr('href');
}
else {
var $permalink = $(location).attr('href');
}
var $permalink = $(location).attr('href'),
$title = $('h1.product_name').text() || $('h2.header');
var $linkhtml = $('<a/>', {
'class':'pin-it-button pinme',
'html': '<img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" />',
'count-layout': 'horizontal',
'style': 'cursor:pointer; position:absolute; bottom:30px; left:0; border:0 none; opacity: 0.4;',
'href': 'http://pinterest.com/pin/create/button/?url=' + $permalink + '&media=' + $(this).attr('src') + '&description=' + $title
});
if ($(this).parent('a')) {
$(this).addClass('pinme').parent('a').after($linkhtml);
}
else {
$(this).addClass('pinme').after($linkhtml);
}
$('.pinme').hover(
function() {
if ($(this).hasClass('pin-it-button')) {
console.log('hello');
$(this).css('opacity', '1');
}
else {
$(this).parent().siblings('.pin-it-button').css('opacity', '1');
}
}, function() {
if ($(this).hasClass('pin-it-button')) {
$(this).css('opacity', '0.4');
}
else {
$(this).parent().siblings('.pin-it-button').css('opacity', '0.4');
}
}
);
});
});
})(jQuery);
【问题讨论】:
-
您只需将
data-pin-no-hover="true"添加到您的图像中。容易得多。 -
@ZackArgyle,回答这个问题,信息值得,您还可以添加一行代码来设置属性(只是为了完整起见)。
-
很高兴发布答案。
标签: javascript jquery google-chrome google-chrome-extension pinterest