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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

See all articles