css怎么实现一条直线向右逐渐延伸显示

html-css07

css怎么实现一条直线向右逐渐延伸显示,第1张

用css3动画去做

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>00</title>

<style>

.xian{ width:50px height:1px background:#000000 margin-top:100px}

.xian{animation: myfirst 5s}

@keyframes myfirst

{

from {width: 50px}

to {width: 1000px}

}

</style>

</head>

<body>

<div class="xian"></div>

</body>

</html>

低版本浏览器不支持css3属性,用谷歌什么的高版本浏览器看。

在制作CSS动画的时候,经常会有这样的需求,

让一个方块 沿着给定的路径 运动。

如果运动路径是不规则的,通过设置 top , left 的属性值,就显得非常困难了。

这时候可以借助svg来实现。

path 元素的形状是通过它的 d 属性 定义的,

d 属性的值,是一个“命令+参数”的序列。

其中, M 20 30 L 160 180 ,包含了2个命令序列,

M 20 30 ,表示将画笔移动到坐标 20,30 处,

L 160 180 ,表示从画笔当前位置,到 160,180 位置画直线。

path元素支持多种命令,可以参考这里, curve commands

html元素的CSS样式属性 offset-path ,表示 偏移路径

通过指定 offset-path 的值为path元素的 d 属性值,我们可以实现元素沿着给定的 path 路径运动。

其中, offset-distance 指定了元素偏移初始位置的百分比。

通过在 @keyframes 中逐帧更改 offset-distance ,可以实现动画效果。

我们修改path的 d 属性为 M10 80 C 40 10, 65 10, 95 80 S 150 150, 180 80 ,

相应的也修改小方块的 offset-path 属性。

就可以实现小方块沿着path运动的效果了。

MDN: paths

MDN: offset-path

MDN: offset-distance

A How-to Guide to SVG Animation

Scalable Vector Graphics (SVG) 2 - Chapter 9: Paths

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Title</title>

<style>

*{

margin: 0

padding: 0

}

.circle{

margin: 30px auto

width: 300px

height: 300px

border: 1px solid red

border-radius:50%

transition: all linear .5s

}

</style>

</head>

<body>

<div></div>

<button id="btn">start</button>

<script>

var circle = document.getElementsByClassName('circle')[0]

var btn = document.getElementById('btn')

var i = true

btn.onclick = function(){

if(i == true){

circle.style.cssText = "width:300pxheight:0pxborder:1px solid redborder-radius:0%"

i = false

}

else{

circle.style.cssText = "width:300pxheight:300pxborder:1px solid redborder-radius:50%transition:all linear .5s"

i = true

}

}

</script>

</body>

</html>