Home > Web Front-end > JS Tutorial > Code for drawing arrow targets using js and css

Code for drawing arrow targets using js and css

小云云
Release: 2018-03-26 16:22:09
Original
1794 people have browsed it

Suppose I want to draw a picture similar to an arrow target now, with three circles. Maybe you can write them directly in HTML. This article mainly shares with you the code for drawing an arrow target using js and css. I hope it can help. Everyone.

//html部分<body>
    <p class="circle0">
        <p class="circle1">
            <p class="circle2">
            </p>
        </p>
    </p></body>
Copy after login

What about making a circle centered and nesting circles inside it?
Position is used here. The values ​​of position are fixed, static, relative, absolute, and inherit.
Among them, static is the default value, fixed is positioned relative to the browser window, and the upper left corner coordinate is (0, 0 );
relative is called relative positioning, which is positioned relative to itself. The self-coordinate is (0, 0), left=20px means offset 20px to the left;
absolute is called absolute positioning, It is positioned relative to the first parent element except static.
Inherit inherits the position attribute value of the parent element.

Here, we do this

.circle0{    /*大圆的大小*/
    /*如何画圆?先是画一个正方形*/
    width:600px;    height:600px;    /*然后设置圆角为50%,就变成一个圆*/
    border-radius:50%;    /*大圆设成relative作为第一个内圆的参考定位容器*/
    position:relative;    /*大圆居中*/
    margin:0 auto;    border:1px solid #000;    background-color:green;}
Copy after login

Continue to handwrite the inner circle css, then you may have to write it one by one, because their sizes are different

.circle1{    width:400px;    height:400px;    border-radius:50%;    /*这是居中的一种方法哦*/
    position:absolute;    top:50%;    left:50%;    /*这样设置之后,圆会偏右下方,应该再往左上方移动一半的自己的宽和高*/
    margin-left:-200px;    margin-top:-200px;    /*这样就居中啦*/  

    border:1px solid #000;    background-color:green;}/*最里面的圆也是这样写,提示:此时它相对第二个圆定位,但是没有什么关系*/.circle2{    width:200px;    height:200px;    border-radius:50%;    position:absolute;    top:50%;    left:50%;    margin-left:-100px;    margin-top:-100px;  

    border:1px solid #000;    background-color:green;}
Copy after login

Although this You can also write down the three round arrow targets you want, but? ? ?

What if I ask you to draw 100 circles? You can't write them one by one, it's such a repetitive work and code, so let's get into the topic of this article, let's write it in js.

Careful students have discovered that the css code of each inner circle is very regular. They are just different in size. Then the center code is divided into two parts. The first half is the same, and the second half is based on itself. Written in half the width and height.

Now that we have found the pattern, we can start writing

/*css部分*/
    /*既然圆的大小是不一样的而且是有规律,那么我们就不写死了,先把一样的写出来吧,有同学可能想为什么不直接也在js中写?第一,因为这都是相同的代码,重复写几次,很耗空间,第二,在js中写这些比直接给它个className耗性能,所以我们能在css中写的还是在css中写,这样也能更好的分离*/
    .circle{        border-radius: 50%;        /*文本居中,为下一篇文章做铺垫*/
        text-align: center;        background-color: green;        border: 1px solid #000;        position: absolute;        top:50%;        left:50%;        cursor: pointer;    }
    /*利用id选择器的优先级比class大,覆盖掉一些属性值,大圆我们希望它在浏览器中也是居中的,而且position是相对定位的,大小也是不要写死,增加灵活性,修改的时候只需要修改几个参数*/
    #circle{        position: relative;        top:0;        left:0;        margin:10px auto;    }
Copy after login
/*js部分*/window.onload=function(){
        //先创建大圆
        var layout=document.createElement("p");        //注意添加class和id的区别哦
        layout.className="circle";
        layout.id="circle";        var n=10;//n个圆,人为参数一,可根据需要修改
        var interval=30;//圆宽高依次小30px,间距则为15px,且最小的圆直径设为30px,则最大的圆的直径为n个interval,人为参数二,可根据需要修改

        layout.style.width=n*interval+"px";
        layout.style.height=n*interval+"px";        //添加文本
        layout.innerHTML=n;        //把大圆添加进body中
        document.getElementsByTagName("body")[0].appendChild(layout);        //接着创建小圆
        for(var i=1;i<n;i++){            //千万要在循环的时候重新给p指向哦,不然再第二个循环的时候就会出错
            var p=document.createElement("p");
            p.className="circle";            //画圆,返回的子圆作为父圆,继续画圆
            layout=loadp(layout,p,n-i); 
        }
    }    function loadp(parent,child,v){
        child.style.width=30*v+"px";
        child.style.height=30*v+"px";        //注意驼峰
        child.style.marginLeft=-15*v+"px";
        child.style.marginTop=-15*v+"px";        //添加文本
        child.innerHTML=v;        parent.appendChild(child);        //返回子圆哦
        //如果上面没有在每一次循环中重新指向一个新的p,在第二次循环的时候调用本函数就是在修改第一个内圆的属性值,而且在appendChild的时候发生错误
        return child;
    }
Copy after login

ok, so we don’t have to use such lengthy css and html to write, and use the patterns among them to directly use Isn’t it very convenient to use js? Hehe. Attached is the rendering of n=20, interval=30px:
Code for drawing arrow targets using js and css

The above is the detailed content of Code for drawing arrow targets using js and css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template