博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mootools_MooTools LinkAlert简介
阅读量:2511 次
发布时间:2019-05-11

本文共 4829 字,大约阅读时间需要 16 分钟。

mootools

One of my favorite Firefox plugins is called . LinkAlert shows the user an icon when they hover over a special link, like a link to a Microsoft Word DOC or a PDF file. I love that warning because I hate the surprise of opening a PDF file. The problem with LinkAlert in its current state is that it's only available with Firefox...until now. Armed with my favorite JavaScript library, MooTools, I've duplicated the functionality of LinkAlert.

我最喜欢的Firefox插件之一叫做 。 将鼠标悬停在特殊链接上时,LinkAlert会向用户显示一个图标,例如指向Microsoft Word DOC或PDF文件的链接。 我喜欢这个警告,因为我讨厌打开PDF文件的惊奇。 LinkAlert当前状态的问题是,它仅在Firefox中可用...直到现在。 借助我最喜欢JavaScript库MooTools,我复制了LinkAlert的功能。

MooTools JavaScript类 (The MooTools JavaScript Class)

var LinkAlert = new Class({	//implements	Implements: [Options,Events],	//options	options: {		container: document.body,		extensions: {},		offsets: { x:6, y:-15 },		preloadImages: true,		protocols: {},		sticky: false,		onShow: function(el,image) {			this.image.setStyle('display','');		},		onHide: function(el,image) {			this.image.setStyle('display','none');		}	},		//initialization	initialize: function(options) {		//set options		this.setOptions(options);		this.extensions = new Hash(this.options.extensions);		this.protocols = new Hash(this.options.protocols);		this.image = new Element('img',{			src: '',			styles: {				position: 'absolute',				top: -200,				left: -200,				visibility: 'hidden'			}		}).inject(document.id(document.body));		//preload images, if necessary		if(this.options.preloadImages) { this.preloadImages(); }		//add the listeners		this.addListeners(this.extensions,'$=','extensions');		this.addListeners(this.protocols,'^=','protocols');	},		//give direction to links	addListeners: function(items,sep,type) {		//for every item		items.each(function(image,extension,type) {			//for every link of that type			$$('a[href' + sep + '\'' + extension + '\']').each(function(el) {				//if the same protocol, don't bother showing				if(type == 'protocol' && window.location.protocol == extension + ':') { return; }				//add the show/hide events				el.addEvents({					mouseenter: function(e) {						//position the image and give it the proper SRC						this.reposition(e).set('src',image);						//fire the event						this.fireEvent('show',[el,image]);					}.bind(this),					mouseleave: function(e) {						//fire the event						this.fireEvent('hide',[el,image]);					}.bind(this)				});				//sticky?				if(this.options.sticky) {					el.addEvent('mousemove',function(e) {						this.reposition(e);					}.bind(this));				}			},this);		},this);	},		//repositions	reposition: function(e) {		return this.image.setStyles({			left: e.page.x + this.options.offsets.x,			top: e.page.y + this.options.offsets.y		});	},		//preloads necessary images	preloadImages: function() {		var images = [this.protocols.getValues(),this.extensions.getValues()].flatten();		new Asset.images(images);	}});

Options for LinkAlert include:

LinkAlert的选项包括:

  • container: (defaults to document.body) The container to search for links within.

    container :( 默认为document.body)用于在其中搜索链接的容器。

  • extensions: (defaults to {}) An object container file extensions and images to look for.

    扩展名:( 默认为{})要查找的对象容器文件扩展名和图像。

  • offsets: (defaults to { x:6, y:-15 }) The x and y offset from the mouse the icon should display.

    offsets :( 默认为{x:6,y:-15})图标应显示的与鼠标的x和y偏移量。

  • preloadImages: (defaults to false) Whether or not to preload all file extension and protocol icons.

    preloadImages:( 默认为false)是否预加载所有文件扩展名和协议图标。

  • protocols: (defaults to {}) An object container protocols and images to look for.

    协议:( 默认为{})要查找的对象容器协议和图像。

  • sticky: (defaults to false) Defines whether the icon should follow the mouse while the mouse is hovered over a link.

    sticky :( 默认为false)定义当鼠标悬停在链接上时图标是否应该跟随鼠标。

Events for LinkAlert include:

LinkAlert的事件包括:

  • Show: Fires when an icon shows.

    显示:图标显示时触发。

  • Hide: Fires when an icon hides.

    隐藏:隐藏图标时触发。

样品用量 (Sample Usage)

/* do it */window.addEvent('domready', function() {	var graphicsDir = 'link-alert/';	var context = new LinkAlert({		extensions: {			doc: graphicsDir + 'word.png',			js: graphicsDir + 'js.png',			pdf: graphicsDir + 'pdf.png',			txt: graphicsDir + 'text.gif',			xls: graphicsDir + 'xls.png',			jpg: graphicsDir + 'jpg.png'		},		protocols: {			ftp: graphicsDir + 'ftp.png',			https: graphicsDir + 'secure.png',			'javascript:': graphicsDir + 'js.png'		},		preloadImages: true,		sticky: true,		onShow: function(el,image) {			this.image.fade('in');		},		onHide: function(el,image) {			this.image.fade('out');		}	});});

I use a few of the options and custom show and hide functionality.

我使用了一些选项以及自定义显示和隐藏功能。

I don't know if this plugin has practical usage for all users but I'll definitely be using it for sites that are in-development. Have any any other uses for this? Share them!

我不知道该插件是否对所有用户都实用,但是我肯定会在开发中的网站上使用它。 还有其他用途吗? 分享他们!

翻译自:

mootools

转载地址:http://iupwd.baihongyu.com/

你可能感兴趣的文章
Java魔法堂:找外援的利器——Runtime.exec详解
查看>>
mysql数据库存放路径
查看>>
TestNG(五)常用元素的操作
查看>>
解决 Visual Studio 点击添加引用无反应的问题
查看>>
通过镜像下载Android系统源码
查看>>
python字符串格式化 %操作符 {}操作符---总结
查看>>
windows 不能在 本地计算机 启动 Apache
查看>>
iOS开发报duplicate symbols for architecture x86_64错误的问题
查看>>
Chap-6 6.4.2 堆和栈
查看>>
【Java学习笔记之九】java二维数组及其多维数组的内存应用拓展延伸
查看>>
C# MySql 连接
查看>>
sk_buff Structure
查看>>
oracle的级联更新、删除
查看>>
多浏览器开发需要注意的问题之一
查看>>
Maven配置
查看>>
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>