如何为每个帖子做一个"喜欢"按钮而不去永久链接页面?我的意思是,即使是在主页上,我可以用哪个脚本在这个页面上画出像心脏这样的东西呢?(鼠标移到照片上,有一个心形点赞)
我已经创建了一个教程:http://like-button.tumblr.com。它消除了人们在实现它时遇到的问题,使它成为一个简单的剪切和粘贴。
要添加Like功能,您可以使用以下URL并将其设置为不可见<iframe>
的src
属性:
http://www.tumblr.com/<command>/<oauthId>?id=<postId>
-
<command>
:like
或unlike
-
<oauthId>
:{ReblogURL}
的后8个字符 -
<postId>
:{PostID}
的例子:
http://www.tumblr.com/like/fGKvAJgQ?id=16664837215
在</head>
之前剪切并粘贴以下代码块到您的主题中。这将给你的每篇文章一个喜欢按钮,看起来像默认的Tumblr灰色心脏。当你将鼠标悬停在它上面或点击它时,它会变成红色。如果你再次点击它,它会再次变成灰色,并删除点赞。
<style>
.my-like {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAARCAYAAAA/mJfHAAABH0lEQVQ4y62T30vCUBiGv/9YuhBLkCA08FdogRFFYFEUhhZNCCQoSESiIOii68pl5qV6s8Eb7+SMHXNs6S7ejZ3zvA+ccT4BICofvS88dJ7w8vqG8WQC754K17lPjrx3z3l8D4YoVaqIrWbcJNbzaHefNZjfXPdy5b0jsO/IRqMxUpmSBnhz2bx1QL79GPbpEePmzhdSyW8fBDL0SK68HwiGCT2S3NiKREaPzP7QRRNPZSHpwm4kMnqkYbQikdEjZv8HK2ubS4nY75mD6WU8qzeXkrHvToBlWSjuHC4kYo99V8bwBnM0/iMiz542myq2bSPskcmR/zPos7lvP8Lv/nGd+/N6c2Xq2KcXhiY6qV1rxwotU3n/NHF8fgW+g9hfsHJlJUYljcgAAAAASUVORK5CYII=) !important;
height:17px;
width:19px;
cursor:pointer;
display:inline-block;
vertical-align:top;
}
.my-liked, .my-like:hover {
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAARCAYAAAA/mJfHAAABSklEQVQ4y2P4//8/Awy/O3fu/905c/4/2779/69Pn/4jy8Hwz/fv/z/buvX/vfnz/7+/eBFFDZj4cOXK/8O+Pv+36+rA8W4zs/8Ply1DUXx/4cL/u0yMUdQdCQ76/+nWLbA6hq+PH//fbW6OogAZ3+zvByu81t6OU80ea6v/P16//s9wqboKpyIYPhYeTlDN1abG/wz7HR0JKiQGH3Bz+8+ww0CfKobtMjb6z0ANg+CGgQKPKt50dfnPcL6wkCqGXaoo/8/w5tgxyg3T0wUnYHBiPJuZSZFhF8pK/8NzACjrgKKWHINAOef3168Iw0D429OnGFmKEAZlJVDKR8mbMAyy4XRqClEGnc3J+f/nxw/MjI6OQflxh6EBzvR0Z9o0rCUKVsNA+MuD+/9PJiSgGHQmPf0/KDhw6cFpGAy/OnAAbOibEyf+E1ILAFBjDrchm7KrAAAAAElFTkSuQmCC) !important;
height:17px;
width:19px;
cursor:pointer;
display:inline-block;
vertical-align:top;
}
</style>
<script>
window.onload = function () {
document.body.insertAdjacentHTML( 'beforeEnd', '<iframe id="my-like-frame" style="display:none;"></iframe>' );
document.addEventListener( 'click', function ( event ) {
var myLike = event.target;
if( myLike.className.indexOf( 'my-like' ) > -1 ) {
var frame = document.getElementById( 'my-like-frame' ),
liked = ( myLike.className == 'my-liked' ),
command = liked ? 'unlike' : 'like',
reblog = myLike.getAttribute( 'data-reblog' ),
id = myLike.getAttribute( 'data-id' ),
oauth = reblog.slice( -8 );
frame.src = 'http://www.tumblr.com/' + command + '/' + oauth + '?id=' + id;
liked ? myLike.className = 'my-like' : myLike.className = 'my-liked';
};
}, false );
};
</script>
然后将下面的按钮代码剪切并粘贴到您想要的喜欢按钮的主题中(这必须在您的{block:Posts}
块中)。
<div class="my-like" data-reblog="{ReblogURL}" data-id="{PostID}"></div>
从页面上看,他们正在使用javascript:
$('a.like-link').click(function() {
var post = $(this).closest('.post');
var id = post.attr('id');
var oath = post.attr('rel').slice(-8);
var like = 'http://www.tumblr.com/like/'+oath+'?id='+id;
$('#likeit').attr('src', like);
}
所以post
只是这篇文章的HTML元素,然后他们简单地得到那篇文章的id
和8个字符的代码,需要点赞或转发一篇文章。他们把所有这些,扔进一个URL,并将其设置为页面上的iFrame的来源(#likeit
)