java ajax 点赞功能_jQuery+ajax实现文章点赞功能的方法

本文实例讲述了jQuery+ajax实现文章点赞功能的方法。分享给大家供大家参考,具体如下:

前几日有童鞋问我索要本站右上角的点赞功能,麦葱左思右想,决定把这功能分享出来,希望此功能对各位会带来方便哦。

代码很简单,jQuery+php实现的。

jQuery代码:

jQuery(document).ready(function($) {

$(".zan").click(function(e){

var $i=$(".zan i"), $b=$("").text("+1"), n=parseInt($i.text());

$b.css({

"bottom":0,

"z-index":999,

});

$i.text(n+1);

$(".zan").append($b);

$b.animate({"bottom":100,"opacity":0},1000,function(){$b.remove();});

var d = setInterval(function(){

clearInterval(d);

if($(".zan b").length == 1){

$.post("",{zan:$i.text()})

}

},1000)

e.stopPropagation();

});

});

php代码:

$path = "zan.txt";

if(isset($_POST['zan'])){

$num = (int)$_POST['zan'];

$save = fopen($path,"w");

fwrite($save,$num);

fclose($save);

echo "good";

exit();

$zan = file_exists($path) ? intval(file_get_contents($path)) : 0;

}

?>

html代码:

看官们给了 <?php echo $zan; ?> 个赞

配上合适的css样式:

.main { position: relative; font-size: 10pt; line-height: 18px; margin: 40px auto; width: 800px; height: 170px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none;}

.zan { position: absolute; top: 20%; left: 45%; width: 160px; height: 110px; background: url("zan.jpg") center no-repeat; cursor: pointer; opacity: 0.85; }

.zan:active { opacity: 1; }

.zan em { position: absolute; color: #333; font-style: normal; bottom: -15px; width: 100%; text-align: center; }

.zan i { font-style: normal; color: #E94F06; }

.zan b { position: absolute; color: #E94F06; font-style: normal; bottom: -15px; width: 100%; text-align: center; }

就是这样,简单吧!

下面是完整代码:

$path = "zan.txt";

if(isset($_POST['zan'])){

$num = (int)$_POST['zan'];

$save = fopen($path,"w");

fwrite($save,$num);

fclose($save);

echo "good";

exit();

}

$zan = file_exists($path) ? intval(file_get_contents($path)) : 0;

?>

我要点赞

.main { position: relative; font-size: 10pt; line-height: 18px; margin: 40px auto; width: 800px; height: 170px; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none;}

.zan { position: absolute; top: 20%; left: 45%; width: 160px; height: 110px; background: url("zan.jpg") center no-repeat; cursor: pointer; opacity: 0.85; }

.zan:active { opacity: 1; }

.zan em { position: absolute; color: #333; font-style: normal; bottom: -15px; width: 100%; text-align: center; }

.zan i { font-style: normal; color: #E94F06; }

.zan b { position: absolute; color: #E94F06; font-style: normal; bottom: -15px; width: 100%; text-align: center; }

看官们给了 <?php echo $zan; ?> 个赞

jQuery(document).ready(function($) {

$(".zan").click(function(e){

var $i=$(".zan i"), $b=$("").text("+1"), n=parseInt($i.text());

$b.css({

"bottom":0,

"z-index":999,

});

$i.text(n+1);

$(".zan").append($b);

$b.animate({"bottom":100,"opacity":0},1000,function(){$b.remove();});

var d = setInterval(function(){

clearInterval(d);

if($(".zan b").length == 1){

$.post("",{zan:$i.text()})

}

},1000)

e.stopPropagation();

});

});

标题都说了是无上限点赞的,So,麦葱告诉大家个小诀窍:

/* 怒赞 */

jQuery.noConflict();

function zan() {

setInterval(function () {

jQuery(".zan").click();

zan();

}, 600)

}

zan();

当然,如果你使用了加速乐防CC(麦葱就是),怒赞请求量过多,会被屏蔽掉哦!除非你取消掉jQuery里的POST

希望本文所述对大家jQuery程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值