css3怎么做出按下按钮就会有爆炸的效果

html-css08

css3怎么做出按下按钮就会有爆炸的效果,第1张

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

<!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>

这个一般用画布做,然后用一个二维数组来排列, 类似一个像素盘,用0和1代表是否填充,比如数字0的数组就是

[[1,1,1,1,1],

[1,0,0,0,1],

[1,0,0,0,1],

[1,1,1,1,1]]

这样渲染以后就是一个长方形,文字类似....

background-image不支持CSS3 transition

background-image 不支持CSS3 transition ,而CSS3 gradient渐变作为背景图片存在的时候,下面的CSS设置是不会有过渡效果的。

.gradient {

background-image: linear-gradient(to right, olive, green)

transition: background-image 0.5s linear

}

.gradient:hover {

background-image: linear-gradient(to right, green, purple)

}

鼠标hover会发现渐变的变化是很唐突的,一点过渡效果也没有。

下面问题来了,如果我们希望实现渐变hover时候有过渡变化的效果,该如何实现呢?我这里罗列的几种可行的方法。

二、借助background-position实现渐变过渡

background-image 虽然不支持CSS3 transition 过渡,但是 background-position 支持啊,于是,通过控制背景位置,我们是可以实现渐变过渡效果的。

实现效果如下(鼠标hover):

相关代码如下:

.box {

max-width: 400px

height: 200px

background: linear-gradient(to right, olive, green, purple)

background-size: 200%

transition: background-position .5s

}

.box:hover {

background-position: 100% 0

}

三、借助background-color实现渐变过渡

background-image 虽然不支持CSS3 transition 过渡,但是 background-color 支持啊,于是,通过控制背景颜色,和一个颜色呈现技巧,我们也是可以实现渐变过渡效果的。

鼠标hover前后效果对比:

相关代码如下:

.box {

max-width: 400px

height: 200px

background: olive linear-gradient(to right, rgba(0,255,0,0), rgba(0,255,0,.5))

transition: background-color .5s

}

.box:hover {

background-color: purple

}

四、借助伪元素和opacity实现渐变过渡

借助伪元素创建变换后的渐变效果,通过改变覆盖的渐变的opacity透明度变化实现渐变过渡效果。

下图为hover之后的效果:

相关代码如下:

.box {

max-width: 400pxheight: 200px

background: linear-gradient(to right, olive, green)

position: relative

z-index: 0

}

.box::before {

content: ''

position: absolute

left: 0top: 0right: 0bottom: 0

background: linear-gradient(to right, green, purple)

opacity: 0

transition: opacity .5s

z-index: -1

}

.box:hover::before {

opacity: 1

}

五、结束语

以上就是我所知道的几个方法,当然,肯定还有其他更好的实现,欢迎补充。希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

html 图片的过渡效果

京东年货节特惠来袭!精华礼盒现购超值

京东

广告

《风尚坐火箭学习vue》-- 第十二章:Vue中的动画transition

174阅读·0评论·1点赞

2021年11月26日

image target behaviour 和image target的关系_CSS3中用background-image实现粒子动画效果

26阅读·0评论·0点赞

2020年11月20日

background-image的用法

13.8W阅读·2评论·33点赞

2016年9月9日

background-image 详解

1.0W阅读·1评论·7点赞

2014年11月17日

html文字图片过度效果,如何在图片上应用css3过渡属性?

510阅读·0评论·0点赞

2021年6月11日

HTML CSS 动画 实现图片过渡与变换(图片不超过边框范围局部放大)

1499阅读·1评论·4点赞

2021年11月25日

京东年货节,小年狂欢开启!酵素饮料现购超划算,速戳~

京东

广告

html 图片的过渡效果,3个CSS3图片过渡动画特效

1880阅读·0评论·1点赞

2021年6月11日