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>
弹出框,在头部按下的时候,开启移动模式,在头部松开的时候就禁止移动.其实就这么一句的意思. 当然逻辑也是比较简单的.
这里我们就可能想到了几个变量,移动的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("移动完成") }
移动弹出框完成后,我们给关闭按钮加入关闭事件.
//关闭 var pop_close = pop.getElementsByClassName("pop-close")[0]; pop_close.onclick = function () { pop.parentNode.removeChild(pop); }
好了,简单的弹出框就实现了.同样代码自己可以优化封装,加入其它的功能.兼容性问题可能需要自己去处理一下(IE9以前的版本).
本文属于吴统威的博客, 微信公众号:bianchengderen,QQ群:186659233 的原创文章,转载时请注明出处及相应链接:http://www.wutongwei.com/front/infor_showone.tweb?id=205 ,欢迎大家传播与分享.
标签 :javascript教程javascript实例
android项目没有android.jar这个依赖包的解决办法
已经没有数据
towaywu
一个喜欢技术,走在创业路上的屌丝!欢迎一起交流
编程的人微信公众号: bianchengderen
交流QQ群: 186659233

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools

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.
