RunAway

  • /*
     * @title RunAway
     * @description リンクが逃げる逃げる
     * @author nori (http://twitter.com/5509)
     * @base script hisasann (http://twitter.com/hisasann)
     * @include http://*
     * @license MIT License
     */
    
    (function() {
     
     	var mouseX, mouseY, d = document, dE = d.documentElement,
     		rate = 60,
     		instance = 3000,
     		anchors = d.getElementsByTagName('a');
     		
     	addEvent(d, 'mousemove', function(e) {
     		mouseY = e.pageY ? e.pageY : e.clientY + dE.scrollTop;
     		mouseX = e.pageX ? e.pageX : e.clientX + dE.scrollLeft;
     	});
     	
     	mover(anchors, instance, rate);
     	
    	function mover(selector, instance, rate) {
    		var firstPointX = [],
    			firstPointY = [];
    			
    	 	for ( var i=0; i<anchors.length; i++ ) {
    			firstPointX[i] = anchors[i].offsetLeft;
    			firstPointY[i] = anchors[i].offsetTop;
    			
    			var span = d.createElement('span');
    			setStyles(span, {
    				display: 'inline-block',
    				width: anchors[i].offsetWidth+'px'
    			});
    			insertAfter(anchors[i].parentNode, span, anchors[i]);
    		}					
    
    		(function(){
    			for ( var i=0; i<anchors.length; i++ ) {
    				var elem = anchors[i],
    					offsetY = elem.offsetTop,
    					offsetX = elem.offsetLeft,
    					theta = Math.atan2(offsetY - mouseY, offsetX - mouseX),
    					d = instance / Math.sqrt(Math.pow(mouseX - offsetX, 2) + Math.pow(mouseY - offsetY, 2)),
    					left = parseInt(offsetX) + d * Math.cos(theta) + (firstPointX[i] - offsetX) * 0.1,
    					top = parseInt(offsetY) + d * Math.sin(theta) + (firstPointY[i] - offsetY) * 0.1;
    					
    				if ( !isNaN(top) && !isNaN(left) ) {
    					setStyles(elem, {
    						left: left+'px',
    						top: top+'px'
    					});
    				} else {
    					setStyles(elem, {
    						position: 'absolute'
    					});
    				}
    			}
    			
    			setTimeout(arguments.callee, rate);
    		})();
    	}
     		
     	function addEvent(elm, listener, fn) {
    		try {
    			elm.addEventListener(listener, fn, false);
    		} catch (e) {
    			elm.attachEvent('on' + listener, fn);
    		}
    	}
    	
    	function setStyles(elm, hash) {
    		for ( var c in hash ) {
    			elm.style[c] = hash[c];
    		}
    	}
    
    	function insertAfter(parent, node, referenceNode) {
    		parent.insertBefore(node, referenceNode.nextSibling);
    	}
     
     })();
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2010/07/01 16:06:21 - 2010-07-01
  2. 2010/07/01 16:05:51 - 2010-07-01