Table of Contents
Javascript实现弹出框
消息
Home Web Front-end HTML Tutorial JAVASCRIPT实现弹出框_html/css_WEB-ITnose

JAVASCRIPT实现弹出框_html/css_WEB-ITnose

Jun 24, 2016 am 11:26 AM

Javascript实现弹出框

作者 :towaywu

2016-02-19 08:57:50.0

22 浏览

类别 :编程语言 HTML/CSS/JAVASCRIPT 前端编程

弹出框在页面中是不可缺少的一部分. 我们今天来实现简单的弹出框效果.

首先我们来分析弹出框的部件.简单弹出框分为头,内容,尾部. 头部中有标题和关闭按钮,内容就可以图文,媒体,iframe,flash等等,尾部就是按钮(确认,取消等等),这个例子尾部我就不加入按钮了,这个例子主要是实现弹出框的核心部分.

<!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title></title>    <style>        body, div{            padding: 0;            margin: 0;        }        html, body {            width: 100%;            height: 100%;        }        a {            text-decoration: none;        }        .pop {            border-radius: 5px;            background-color: #fff;            border: #eee 1px solid;            position: absolute;            width: 350px;            left: 35%;            top: 25%;        }        .pop-title {            background-image: linear-gradient(#eee,#efefef);            position: relative;            cursor: pointer;        }        .pop-title h3,.pop-title a{            display: inline-block;        }        .pop-title h3{            font-size: 14px;            margin: 0;            padding: 5px;        }        .pop-title a {            position: absolute;            top: 5px;            right: 5px;        }        .pop-content {            padding: 10px;        }    </style></head><body>    <div>        <div>            <h3 id="消息">消息</h3>            <a href="javascript:;">X</a>        </div>        <div>            弹出框已显示        </div>        <div></div>    </div></body></html>
Copy after login

弹出框,在头部按下的时候,开启移动模式,在头部松开的时候就禁止移动.其实就这么一句的意思. 当然逻辑也是比较简单的.

这里我们就可能想到了几个变量,移动的X,Y坐标,移动的开关和禁止. 然后就是给title加入onmousedown 和 onmouseup事件.

onmousedown事件中主要是开启移动.

onmouseup 事件中逻辑主要是关闭移动,禁止移动弹出框.

接着需要移动,而移动需要时在某个范围内移动.这里我们是在body里面移动. 所以给body加入onmousemove事件. 这里面做的事情就是移动弹出框的位置处理.

在这三个事件中主要是配合了CSS中的position属性和JS中的Event事件中的属性的坐标.

  var pop = document.getElementsByClassName("pop")[0];        var pop_title = pop.getElementsByClassName("pop-title")[0];        var bd = document.body;        var x = 0;        var y = 0;        var ismove = false; // 是否开启移动        var downx = 30;        var downy = 30;        pop_title.onmousedown = function (e) {            x = e.pageX;            y = e.pageY;            downx = e.offsetX;            downy = e.offsetY;            ismove = true;        }        bd.onmousemove = function (e) {            if (ismove) {                var cx = e.pageX - downx;                var cy = e.pageY - downy;                pop.style.left = cx + "px";                pop.style.top = cy + "px";                x = e.x;                y = e.y;            }            e.preventDefault();        }        pop_title.onmouseup = function (e) {            x = e.pageX;            y = e.pageY;            ismove = false;            console.log("移动完成")        }
Copy after login

移动弹出框完成后,我们给关闭按钮加入关闭事件.

//关闭        var pop_close = pop.getElementsByClassName("pop-close")[0];  pop_close.onclick = function () {      pop.parentNode.removeChild(pop); }
Copy after login

好了,简单的弹出框就实现了.同样代码自己可以优化封装,加入其它的功能.兼容性问题可能需要自己去处理一下(IE9以前的版本).

本文属于吴统威的博客, 微信公众号:bianchengderen,QQ群:186659233 的原创文章,转载时请注明出处及相应链接:http://www.wutongwei.com/front/infor_showone.tweb?id=205 ,欢迎大家传播与分享.

标签 :javascript教程javascript实例

android项目没有android.jar这个依赖包的解决办法

已经没有数据

towaywu

一个喜欢技术,走在创业路上的屌丝!欢迎一起交流

编程的人微信公众号: bianchengderen

交流QQ群: 186659233
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML vs. CSS vs. JavaScript: A Comparative Overview HTML vs. CSS vs. JavaScript: A Comparative Overview Apr 16, 2025 am 12:04 AM

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML: Building the Structure of Web Pages HTML: Building the Structure of Web Pages Apr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

See all articles