search
HomeWeb Front-endJS TutorialCode for drawing arrow targets using js and css

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>

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;}

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;}

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;    }
/*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;
    }

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!

Statement
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
Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.