Home Web Front-end HTML Tutorial Drop.js-实用的JavaScript下拉弹出层插件_html/css_WEB-ITnose

Drop.js-实用的JavaScript下拉弹出层插件_html/css_WEB-ITnose

Jun 24, 2016 am 11:27 AM

简要教程

Drop.js是一款JavaScript和CSS实用下拉弹出层插件。该插件基于Tether.js来定位弹出层,可以制作多种弹出层效果。它的特点还有:

  • 弹出层会在页面尺寸改变和滚动时自动更新位置。

  • 弹出层在页面滚动时使用GPU加速来定位。

  • 弹出层之间可以进行嵌套。

  • 弹出层可以在元素的12个位置上显示。

  • 可以自定义是在点击元素、鼠标滑过元素或元素聚焦时显示弹出层。

查看演示       下载插件

使用方法

由于该弹出层插件依赖于tether.js插件,使用该弹出层插件需要在页面中引入tether.min.js、drop.min.js和drop-theme-arrows.css文件。

初始化插件

要初始化一个drop,需要实例化一个Drop对象。

drop = new Drop({  target: document.querySelector('.drop-target'),  content: '** to the future!',  position: 'bottom left',  openOn: 'click'});
Copy after login

你也可以通过自定义“上下文”来创建Drops,在“上下文”中你可以指定弹出层的CSS前缀字符串,默认的前缀是drop。

MyDropContext = Drop.createContext({  classPrefix: 'my-drop'});drop = new MyDropContext({  target: document.querySelector('.my-drop-target'),  content: '** to my new Drop context!'});
Copy after login

在上面的实例化代码中,所有弹出层会通过my-drop-open和my-drop-content来进行渲染,而不是drop-open和drop-content。另外,所有通过data-drop来设置的属性现在都会被替换为通过data-my-drop来设置。

配置参数

下面的参数可以在drop构造函数中使用。

  • target:打开弹出层的目标元素。
  • content:弹出层显示的内容。可以是:

一个DOM元素

HTML字符串

一个返回HTML字符串或DOM元素的函数。content()会在每次弹出层打开时都被调用。

如果该参数没有设置,默认为目标元素的data-${classPrefix}(通常是data-drop)属性的值。

  • position:弹出层的显示位置,可以是:

'top left'

'left top'

'left middle'

'left bottom'

'bottom left'

'bottom center'

'bottom right'

'right bottom'

'right middle'

'right top'

'top right'

'top center'

如果该参数没有设置,默认为目标元素的data-${classPrefix}-position(通常是data-drop-position)属性的值。

  • openOn:指定触发弹出层的事件。如果该参数设置为undefined或null,你需要手动管理drop实例的open()和close()方法。设置为'always'可以在弹出层渲染之后立刻打开。可用的值有:

'click'

'hover'

'focus'

'always'

如果该参数没有设置,默认为目标元素的data-${classPrefix}-openOn(通常是data-drop-openOn)属性的值。

  • constrainToWindow:rugged设置为true,将使用Tether插件的容器列表来在元素超出视口时翻转元素。这会使得原来在下方的弹出层翻转到元素的上方。
  • constrainToScrollParent:Similar to constrainToWindow but for the target element's first scroll parent: the first parent that has overflow: auto or overflow: scroll set, or the body, whichever comes first.
  • classes:为弹出层添加额外的class类。
  • remove:如果设置为true,在关闭弹出层时将它从DOM中移除,再次打开时重新渲染。
  • beforeClose:关闭弹出层前触发的回调函数。如果函数返回false,将不会关闭弹出层。
  • hoverOpenDelay:在mouseover之后打开弹出层的延迟时间,单位毫秒。
  • hoverCloseDelay:在mouseout之后关闭弹出层的延迟时间,单位毫秒。
  • focusDelay:在focus之后打开弹出层的延迟时间,单位毫秒。
  • blurDelay:在blur之后关闭弹出层的延迟时间,单位毫秒。
  • openDelay:同时设置hoverOpenDelay和focusDelay。
  • closeDelay:同时设置hoverCloseDelay和blurDelay。

默认的参数值为:

defaultOptions =    position: 'bottom left'    openOn: 'click'    constrainToWindow: true    constrainToScrollParent: true    classes: ''    hoverOpenDelay: 0    hoverCloseDelay: 50    focusDelay: 0    blurDelay: 50    tetherOptions: {}
Copy after login

方法

下面的方法都可以通过Drop对象实例来调用。

  • open():打开一个弹出层。会为弹出层添加drop-open class。
  • close():关闭一个弹出层。会关闭弹出层的drop-open class。被关闭的弹出层仍然在DOM中。
  • remove():从DOM中移除弹出层。
  • toggle():切换弹出层。
  • isOpened():如果弹出层是打开状态返回true。
  • position():重新定位弹出层。
  • destroy():销毁弹出层和移除所有的事件。

事件

drop实例有以下一些可用绑定的事件。

  • on(eventName, handler, [ctx])
  • off(eventName, [handler])
  • once(eventName, handelr, [ctx])

可触发的事件有:

  • open
  • close

drop弹出层插件的github地址为: http://github.hubspot.com/drop/

来源:jQuery之家

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

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

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.

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.

The Role of HTML: Structuring Web Content The Role of HTML: Structuring Web Content Apr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

See all articles