jQuery+ajax實(shí)現(xiàn)文章點(diǎn)贊功能的方法
本文實(shí)例講述了jQuery+ajax實(shí)現(xiàn)文章點(diǎn)贊功能的方法。分享給大家供大家參考,具體如下:
前幾日有童鞋問(wèn)我索要本站右上角的點(diǎn)贊功能,麥?zhǔn)[左思右想,決定把這功能分享出來(lái),希望此功能對(duì)各位會(huì)帶來(lái)方便哦。
代碼很簡(jiǎn)單,jQuery+php實(shí)現(xiàn)的。
jQuery代碼:
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(); }); });
php代碼:
<?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代碼:
<div class="main"> <div class="zan"><em>看官們給了 <i><?php echo $zan; ?></i> 個(gè)贊</em></div> </div>
配上合適的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; }
就是這樣,簡(jiǎn)單吧!
下面是完整代碼:
<?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>我要點(diǎn)贊</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> 個(gè)贊</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>
標(biāo)題都說(shuō)了是無(wú)上限點(diǎn)贊的,So,麥?zhǔn)[告訴大家個(gè)小訣竅:
/* 怒贊 */ jQuery.noConflict(); function zan() { setInterval(function () { jQuery(".zan").click(); zan(); }, 600) } zan();
當(dāng)然,如果你使用了加速樂(lè)防CC(麥?zhǔn)[就是),怒贊請(qǐng)求量過(guò)多,會(huì)被屏蔽掉哦!除非你取消掉jQuery里的POST
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
相關(guān)文章
jquery的ajax簡(jiǎn)單結(jié)構(gòu)示例代碼
這篇文章主要介紹了jquery的ajax簡(jiǎn)單結(jié)構(gòu),需要的朋友可以參考下2014-02-02jQuery pjax 應(yīng)用簡(jiǎn)單示例
此篇文章給大家分享了jQuery pjax 應(yīng)用的一些簡(jiǎn)單示例,希望可以幫助到大家2018-09-09解決jQuery ajax動(dòng)態(tài)新增節(jié)點(diǎn)無(wú)法觸發(fā)點(diǎn)擊事件的問(wèn)題
在寫ajax加載數(shù)據(jù)的時(shí)候發(fā)現(xiàn),后面添加進(jìn)來(lái)的demo節(jié)點(diǎn)元素,失去了之前的點(diǎn)擊事件,如何解決此問(wèn)題呢?下面小編給大家?guī)?lái)了jQuery ajax動(dòng)態(tài)新增節(jié)點(diǎn)無(wú)法觸發(fā)點(diǎn)擊事件的解決方法,一起看看吧2017-05-05基于jquery實(shí)現(xiàn)的圖片在各種分辨率下未知的容器內(nèi)上下左右居中
這篇文章主要介紹了基于jquery實(shí)現(xiàn)的圖片在各種分辨率下未知的容器內(nèi)上下左右居中的方法,需要的朋友可以參考下2014-05-05jQuery實(shí)現(xiàn)動(dòng)畫效果circle實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)動(dòng)畫效果circle的方法,涉及jquery鼠標(biāo)事件及頁(yè)面動(dòng)畫操作相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08深入理解jquery的$.extend()、$.fn和$.fn.extend()
下面小編就為大家?guī)?lái)一篇深入理解jquery的$.extend()、$.fn和$.fn.extend()。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07淺析jQuery Ajax請(qǐng)求參數(shù)和返回?cái)?shù)據(jù)的處理
這篇文章主要介紹了淺析jQuery Ajax請(qǐng)求參數(shù)和返回?cái)?shù)據(jù)的處理的相關(guān)資料,需要的朋友可以參考下2016-02-02