javascript Modal.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Modal.js相关的知识,希望对你有一定的参考价值。

//--------------------------------------------------------------
// Modal
//--------------------------------------------------------------

export const Modals = (function() {

	function BuildModals() {
		let publicAPIs = {};

		/**
		 * Private Methods
		 */
		function runModals() {
			const modals = [...document.querySelectorAll('[data-modal]')];

			modals.forEach(modal => {
				const modalId = modal.getAttribute('data-modal');
				publicAPIs.modals[modalId] = modal
			});

			// Open
			document.addEventListener('click', e => {
				// bail if not an a tag
				if (e.target.nodeName !== 'A') return;

				// Bail if the hash is not formatted correctly
				if(!isTargetUrl(e.target.hash)) return;

				const hashTarget = (e.target.hash).replace('#', '');

				if(checkForModalId(hashTarget)) {
					e.preventDefault();

					publicAPIs.openModal(hashTarget);
				}
			});

			// Close
			document.addEventListener('click', e => {
				if (e.target.hasAttribute('data-close-modal')) {
					publicAPIs.closeAllModals();
				}
			});
		}

		/**
		 * Check if url is a target url with the #
		 */
		function isTargetUrl(url) {
			const test = new RegExp(/^#/, 'gm');

			return test.test(url);
		}

		// Check if Modal is in the list of registered Modals
		function checkForModalId(id) {
			return publicAPIs.modals.hasOwnProperty(id);
		}

		/**
		 * Public APIs
		 */
		publicAPIs.modals = {};

		publicAPIs.openModal = function(modalId) {
			publicAPIs.modals[modalId].classList.add('is-active');
		}

		publicAPIs.closeAllModals = function() {
			for (let modal in publicAPIs.modals) {
				publicAPIs.modals[modal]
					.classList.remove('is-active');
			}
		}

		publicAPIs.init = function() {
			runModals();
		}

		publicAPIs.init();

		return publicAPIs;
	}

	return BuildModals;

})(window, document);

以上是关于javascript Modal.js的主要内容,如果未能解决你的问题,请参考以下文章

javascript WC-添加到购物车,modal.js

markdown 将内容加载到Modal(JS)中

在动态创建的 iframe 中触发 Javascript

模态框 modal.js

yii2中怎么使用Bootstrap的模态弹出框modal

bootstrap 模态框 modal 插件在一个含有多个页面