红心点赞按钮怎么做

html-css06

红心点赞按钮怎么做,第1张

需要学习JavaScript语言和HTML语言

使用方法

HTML结构

该“点赞”红心按钮的HTML结构是将心形按钮放在一个超链接的后面或下面,它的HTML结果如下:

<div class="feed" id="feed1">

<p>jQuery之家 <a href="http://www.htmleaf.com/">http://www.htmleaf.com/</a></p>

<div class="heart" id="like3" rel="like"></div>

<div class="likeCount" id="likeCount3">24</div>

</div>

CSS样式

整个“点赞”烟花爆炸效果实际上是使用背景图片的连续播放来完成的,它使用一张很长的雪碧(Sprite)图片,然后在点击红心按钮是逐帧进行播放。下面是开始时显示第一帧的CSS样式:

.heart {

background: url(images/web_heart_animation.png)

background-position: left

background-repeat: no-repeat

height: 50px

width: 50px

cursor: pointer

position: absolute

left:-14px

background-size:1450px//实际背景图片尺寸2900px

}

.heart:hover{

background-position: right//显示最后一个红心帧

}

.likeCount{

margin-top: 13px

margin-left: 28px

font-size: 16px

color: #999999

}

下面的代码是逐帧播放红心背景图片,它使用CSS3 keyframe帧动画来完成。

@-webkit-keyframes heartBlast {

0% {background-position: left}

100% {background-position: right}

}

@keyframes heartBlast {

0% {background-position: left}

100% {background-position: right}

}

.heartAnimation {

-webkit-animation-name: heartBlast//webkit内核浏览器

animation-name: heartBlast

-webkit-animation-duration: .8s

animation-duration: .8s

-webkit-animation-iteration-count: 1

animation-iteration-count: 1

-webkit-animation-timing-function: steps(28)//共28个背景图片帧

animation-timing-function: steps(28)

background-position: right

}

JavaScript

在这个“点赞”红心动画特效中使用jQuery代码来使红心按钮在点击时与相应的总点赞数量进行联动。你可以通过ajax来进行数据库操作。当用户点赞时,总数量加1,取消点赞时,总数量减1。

<script>

$(document).ready(function(){

$('body').on("click",'.heart',function(){

var A=$(this).attr("id")

var B=A.split("like")//splitting like1 to 1

var messageID=B[1]

$(this).css("background-position","")

var D=$(this).attr("rel")

$.ajax({

type: "POST",

url: "message_like_ajax.php",

data: dataString,

cache: false,

success: function(data){

$("#likeCount"+messageID).html(data)

if(D === 'like') {

$(this).addClass("heartAnimation").attr("rel","unlike")//applying animation class

}

else{

$(this).removeClass("heartAnimation").attr("rel","like")

$(this).css("background-position","left")

}

})//ajax end

})//heart click end

})

</script>

修改相关的参数,可以达到爆炸效果

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>twitterLove</title>

</head>

<body>

<canvas width="100" height="100" style="border: 1px black solid">

    必须要把style写在内联,不然会变成椭圆。而且width与height要单独写出来

    注意:1,每画一个图形都要提起笔。不然会连在一起

    2,arc的(centerX[圆心横坐标,以父节点的右上角为坐标原点,向左向下建立坐标轴],centerY,r[半径],

    startAngle[起始点的角度。起始以(1,0)表示0,(0,1)表示3/2*PI计算。当设置为逆时针的时候画出来是起点到终点之间的扇形出去中心三角],

    endAngle,anticlockwise(是否逆时针))

    3,画图的时候要计算好坐标

    4,最好以角度值百分比计算。流式布局更能适应各种大小的缩放

    5,注意提取公共函数

    6,要划分步骤

    7,注意上一幅图与下一幅图的关系。用clearRect来清除

</canvas>

<script>

    var canvas=(document.getElementsByTagName("canvas"))[0]//获取绘图区域,是一个正方形区域

    var ctx=canvas.getContext("2d")//获取画笔

    //必须写在外面,公共的

    var centerX=(canvas.width)*0.5

    var centerY=(canvas.height)*0.5//获取中心,是一个正方形

    //获得一个爱心

    function love(color,centerX,centerY,size){

        ctx.beginPath()

        //上半部

        var smallRadius=Math.round(centerX/size)//小圆的半径

        var smallMoveLen=smallRadius*Math.sqrt(3)/2

        ctx.fillStyle=color||"red"

        ctx.arc(centerX-smallMoveLen,centerY,smallRadius,Math.PI*7/4,Math.PI,true)

        ctx.arc(centerX+smallMoveLen,centerY,smallRadius,0,Math.PI*5/4,true)

        //下半部

        var bigRadius=smallRadius*2.73

        ctx.arc(centerX+smallMoveLen,centerY,bigRadius,Math.PI,Math.PI*0.6,true)

        ctx.arc(centerX-smallMoveLen,centerY,bigRadius,Math.PI*0.4,0,true)

        ctx.fill()

        ctx.closePath()

    }

    love("grey",centerX,centerY,8)//默认灰色

    //注册监听

    canvas.addEventListener("click",function () {

        if(ctx.fillStyle=="#808080"){//表示为灰色

           // alert("点赞")

            //1,爱心消失//清除画板内容

            ctx.clearRect(0,0,centerX*2,centerY*2)

            //动态图

            var bigRadius=centerX/2

            var midRadius=centerX/5

            var smallRadius=centerX/10

            //2,小圆,圆心都是中心位置

            setTimeout(function () {

                ctx.beginPath()

                ctx.fillStyle="#FF6BDB"

                ctx.arc(centerX,centerY,smallRadius,0,2*Math.PI,false)

                ctx.fill()

                ctx.closePath()//必须要提笔。不然和前面一只笔相当于没提起来

            },100)

            //3,中圆

            setTimeout(function () {

                ctx.beginPath()

                ctx.fillStyle="#9FD5FF"

                ctx.arc(centerX,centerY,midRadius,0,2*Math.PI,false)

                ctx.fill()

                ctx.closePath()

            },200)

            //4,大圆

            setTimeout(function () {

                ctx.beginPath()

                ctx.fillStyle="#FF84A6"

                ctx.arc(centerX,centerY,bigRadius,0,2*Math.PI,false)

                ctx.fill()

                ctx.closePath()

            },300)

            //5,小爱心

            setTimeout(function () {

                ctx.clearRect(0,0,centerX*2,centerY*2)

                ctx.beginPath()

                ctx.fillStyle="#FF84A6"

                ctx.arc(centerX,centerY,bigRadius,0,2*Math.PI,false)

                ctx.fill()

                ctx.closePath()

                ctx.beginPath()

                ctx.fillStyle="#ffffff"

                ctx.arc(centerX,centerY,midRadius*2,0,2*Math.PI,false)

                ctx.fill()

                ctx.closePath()

                ctx.beginPath()

                love("purple",centerX,centerY,16)

                ctx.closePath()

            },400)

            //6,四周小圆

            setTimeout(function () {

                ctx.clearRect(0,0,centerX*2,centerY*2)

                var e=bigRadius/(Math.sqrt(2))

                var centerXArr=[centerX-bigRadius,centerX-e,centerX,centerX+e,centerX+bigRadius,centerX+e,centerX,centerX-e]

                var centerYArr=[centerY,centerY-e,centerY-bigRadius,centerY-e,centerY,centerY+e,centerY+bigRadius,centerY+e]

                for(var i=0i<8i++){

                    ctx.beginPath()

                    ctx.fillStyle="blue"

                    ctx.arc(centerXArr[i],centerYArr[i],smallRadius/4,0,2*Math.PI,false)

                    ctx.fill()

                    ctx.closePath()

                }

                ctx.fillStyle="#ff0000"

                love("ff0000",centerX,centerY,8)//red

            },500)

            //7,红色大爱心

            setTimeout(function () {

                ctx.clearRect(0,0,centerX*2,centerY*2)

                ctx.beginPath()

                love("ff0000",centerX,centerY,8)

                ctx.closePath()

            },600)

        }

        else if(ctx.fillStyle=="#ff0000"){//表示为红色

           // alert("取消赞")

            ctx.fillStyle="#ff0000"

            love("#808080",centerX,centerY,8)

        }

    },false)

</script>

</body>

</html>