这样就产生了一个圆角div
实现原理:原理其实很简单,在div的top和bottom 加上三条线,用这三条线的不同长度来产生圆角的效果。

实现过程: 如何实现这三条线呢。 用<b> 这个元素,将其高">
search
HomeWeb Front-endJS TutorialJavascript rounded div implementation code_javascript skills

Nowadays, the rounded corners are generally controlled by pictures. This method has its advantages (the resulting rounded corners are smooth). But at the same time, he also requires matching pictures. If he wants to dynamically change the style and color of the div, it will be a bit difficult. There is also the use of js to achieve it.
The implemented calling code is as follows

Copy code The code is as follows:

var objDiv = getRoundDiv.call(document,"solid 1px yellow","#dddddd")
objDiv.Div.style.width="100px";
objDiv.Content.style.margin="6 6 6 6 "
objDiv.Content.innerText="This is a rounded div test"
document.body.appendChild(objDiv.Div);


This way A rounded div
is generated. Implementation principle: The principle is actually very simple. Add three lines to the top and bottom of the div, and use the different lengths of these three lines to produce the effect of rounded corners.

Implementation process: How to implement these three lines. Use the element and set its height to 1px. If you want to display borders, add left and right borders to them. After adding the lines, put the content div and these three lines in a container. This container is also a div. Finally, a div class is returned. This class contains two attributes. One is a container div. Through this container div, the position, size, height and other attributes of the graphics can be controlled. Another attribute is the content div, through which you can set the content, margin, font color, background color, font size, and other attributes of this div.

Note: Calling the getRoundDiv method requires passing a method context. My understanding is that the method context is equivalent to a pointer pointing to the object on which the method is called. Why use this method context? For example, if you want to create a new rounded div in the popup document generated by IE's creatPopup method, since the popup can only load controls created by itself, the popup object can be passed into the method and become the object pointed to by the method context. There are two methods of passing context function.call(obj,"arg1","arg2") similar to this. The other is function.apply(obj,arguments)

The detailed code is as follows:
Copy code The code is as follows :

/**************************************************************************/
/*RoundDiv.js 产生一个圆角div
调用前需设置函数上下文(上下文是指,要创建div的窗口) 例如 var objDiv = getRoundDiv.call(document,"","#dddddd")
函数参数argBorderStyle: 边框样式,字符串 例如 "1px solid black"
函数参数argBgColor: 背景颜色,字符串 例如 "#ffffff"
现在只支持边框为1像素 如果超过1像素产生的图形会比较奇怪
如果不设置边框 则没有边框 可以正常使用
本函数返回的是一个RoundDiv自定义类
如果要设置div的内容请用 obj.Content.innerHtml 或 obj.Content.innerText设置
如果要设置div的高度请用 obj.Div.style.width obj.Div.style.height设置
*/
/**************************************************************************/
/**************************************************************************/
//取得一个圆角div
function getRoundDiv(argBorderStyle,argBgColor){

    //创建元素
    var divPane =this.createElement("div")
    var divContent =this.createElement("div")
    var divContentMax =this.createElement("div")
    var bTop =this.createElement("b")
    var bBottom =this.createElement("b")
    var bTop1 =this.createElement("b")
    var bTop2 =this.createElement("b")
    var bTop3 =this.createElement("b")
    var bTop4 =this.createElement("b")
    var bBottom1 =this.createElement("b")
    var bBottom2 =this.createElement("b")
    var bBottom3 =this.createElement("b")
    var bBottom4 =this.createElement("b")

    //背景设置
    divPane.style.backgroundColor=argBgColor;
    divContent.style.backgroundColor=argBgColor;
    divContentMax.style.backgroundColor=argBgColor;

    bTop1.style.backgroundColor=argBgColor;
    bTop2.style.backgroundColor=argBgColor;
    bTop3.style.backgroundColor=argBgColor;
    bTop4.style.backgroundColor=argBgColor;
    bBottom1.style.backgroundColor=argBgColor;
    bBottom2.style.backgroundColor=argBgColor;
    bBottom3.style.backgroundColor=argBgColor;
    bBottom4.style.backgroundColor=argBgColor;
    bTop.style.backgroundColor="#ffffff";
    bBottom.style.backgroundColor="#ffffff";

    //样式设置
    bTop.style.overflow="hidden";
    bBottom.style.overflow="hidden";
    bTop1.style.overflow="hidden";
    bTop2.style.overflow="hidden";
    bTop3.style.overflow="hidden";
    bTop4.style.overflow="hidden";
    bBottom1.style.overflow="hidden";
    bBottom2.style.overflow="hidden";
    bBottom3.style.overflow="hidden";
    bBottom4.style.overflow="hidden";

    bTop.style.display="block";
    bBottom.style.display="block";
    bTop1.style.display="block";
    bTop2.style.display="block";
    bTop3.style.display="block";
    bTop4.style.display="block";
    bBottom1.style.display="block";
    bBottom2.style.display="block";
    bBottom3.style.display="block";
    bBottom4.style.display="block";

    
    //高度设置
    divContent.style.height="100%";
    divContentMax.style.height="100%";

    bTop1.style.height="1px";
    bTop2.style.height="1px";
    bTop3.style.height="1px";
    bTop4.style.height="2px";

    bBottom1.style.height="1px";
    bBottom2.style.height="1px";
    bBottom3.style.height="1px";
    bBottom4.style.height="2px";

    
    //边框设置
    divContentMax.style.borderLeft=argBorderStyle
    divContentMax.style.borderRight=argBorderStyle

    bTop1.style.borderLeft=argBorderStyle;
    bTop1.style.borderRight=argBorderStyle;
    bTop1.style.borderTop=argBorderStyle;
    bTop2.style.borderLeft=argBorderStyle;
    bTop2.style.borderRight=argBorderStyle;
    bTop3.style.borderLeft=argBorderStyle;
    bTop3.style.borderRight=argBorderStyle;
    bTop4.style.borderRight=argBorderStyle;
    bTop4.style.borderLeft=argBorderStyle;
    bBottom1.style.borderLeft=argBorderStyle;
    bBottom1.style.borderRight=argBorderStyle;
    bBottom1.style.borderTop=argBorderStyle;
    bBottom2.style.borderLeft=argBorderStyle;
    bBottom2.style.borderRight=argBorderStyle;
    bBottom3.style.borderLeft=argBorderStyle;
    bBottom3.style.borderRight=argBorderStyle;
    bBottom4.style.borderLeft=argBorderStyle;
    bBottom4.style.borderRight=argBorderStyle;

    
    //空白间距设置
    bTop1.style.margin="0 4px 0 4px"
    bTop2.style.margin="0 3px 0 3px"
    bTop3.style.margin="0 2px 0 2px"
    bTop4.style.margin="0 1px 0 1px"

    bBottom1.style.margin="0 4px 0 4px"
    bBottom2.style.margin="0 3px 0 3px"
    bBottom3.style.margin="0 2px 0 2px"
    bBottom4.style.margin="0 1px 0 1px"

    //控件拼装
    bTop.appendChild(bTop1);
    bTop.appendChild(bTop1);
    bTop.appendChild(bTop2);
    bTop.appendChild(bTop3);
    bTop.appendChild(bTop4);    
    bBottom.appendChild(bBottom4);
    bBottom.appendChild(bBottom3);
    bBottom.appendChild(bBottom2);
    bBottom.appendChild(bBottom1);

    divContentMax.appendChild(divContent)
    divPane.appendChild(bTop)
    divPane.appendChild(divContentMax)
    divPane.appendChild(bBottom)
    var objRoundDiv = new RoundDiv();
    objRoundDiv.Div=divPane;
    objRoundDiv.Content=divContent;
    return objRoundDiv;

}
/**************************************************************************/
/**************************************************************************/
//自定义类(用来装载div对应内容)
function RoundDiv(){
    this.content=0;//div内容
    this.div=0;//div容器
}
/**************************************************************************/

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

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Is Python or JavaScript better?Is Python or JavaScript better?Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

How do I install JavaScript?How do I install JavaScript?Apr 05, 2025 am 12:16 AM

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.

How to send notifications before a task starts in Quartz?How to send notifications before a task starts in Quartz?Apr 04, 2025 pm 09:24 PM

How to send task notifications in Quartz In advance When using the Quartz timer to schedule a task, the execution time of the task is set by the cron expression. Now...

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)