jQuery+ajax+php实现文章点赞功能的方法

2019-03-2209:51:52WEB前端开发Comments2,957 views6字数 3103阅读模式

本文实例讲述了jQuery+ajax实现文章点赞功能的方法。分享给大家供大家参考,具体如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

前几日有童鞋问我索要本站右上角的点赞功能,麦葱左思右想,决定把这功能分享出来,希望此功能对各位会带来方便哦。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

代码很简单,jQuery+php实现的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

jQuery代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

jQuery(document).ready(function($) {
$(".zan").click(function(e){
var $i=$(".zan i"), $b=$("<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();
});
});文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

php代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

<?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;
}
?>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

html代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

<div class="main">
<div class="zan"><em>看官们给了 <i><?php echo $zan; ?></i> 个赞</em></div>
</div>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

配上合适的css样式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

.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; }文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

就是这样,简单吧!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

下面是完整代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

<?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;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>我要点赞</title>
<style>
.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; }
</style>
</head>
<body>
<div class="main">
<div class="zan"><em>看官们给了 <i><?php echo $zan; ?></i> 个赞</em></div>
</div>
<script src="jquery.min.js"></script>
<script>
jQuery(document).ready(function($) {
$(".zan").click(function(e){
var $i=$(".zan i"), $b=$("<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();
});
});
</script>
</body>
</html>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

标题都说了是无上限点赞的,So,麦葱告诉大家个小诀窍:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

/* 怒赞 */
jQuery.noConflict();
function zan() {
setInterval(function () {
jQuery(".zan").click();
zan();
}, 600)
}
zan();文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

当然,如果你使用了加速乐防CC(麦葱就是),怒赞请求量过多,会被屏蔽掉哦!除非你取消掉jQuery里的POST文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

希望本文所述对大家jQuery程序设计有所帮助。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10285.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/10285.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定