You are not logged in.

  • Login

Saturday, March 5th 2011, 3:04pm

Content

Tags

bubble, preview, thumbshots, vorschau

Abstract

Hier stelle ich ein generisches Script vor, mit dem ihr beliebige Inhalte unter einem Link platzieren könnt.

Article

1. Script


JavaScript Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
 * Bubble Helper Class
 *
 * example usage
 * Bubble.init("externalURL", function (D) {
 *   return D.href + '<br /><img src="easy-coding-logo.png">';
 * });
 *
 * @author      Torben Brodt <easy-coding.de>
 * @url		http://trac.easy-coding.de/trac/wcf/wiki/Thumbshots
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-3.0.html>
 */
var Bubble = {
	Cursor: {
		x: 0,
		y: 0
	},
	Offset: {
		x: 0,
		y: 0
	},
	blacklist : {},
	div: document.getElementById("BubbleDiv"),
	init: function (ParentID, showRef) {
		Bubble.create();
		Bubble.addEvent(window, "mousemove", Bubble.getCurPos);
		Bubble.addEvent(window, "resize", Bubble.getOffset);
		Bubble.addEvent(window, "scroll", Bubble.getOffset);
		var B = Bubble.getElementsByClassName(ParentID), A = B.length;
		for (i = 0; i < A; i++) {
			if(Bubble.blacklist.href && !B[i].href.match(Bubble.blacklist.href)) {
				Bubble.addEvent(B[i], "mouseover", function () {
					Bubble.show(this, showRef);
				});
				Bubble.addEvent(B[i], "mouseout", function () {
					Bubble.hide();
				});
			}
		}
	},
	/**
	 * currently only href is supported
	 */
	disallow: function (attr, str) {
		Bubble.blacklist.href = str;
	},
	show: function (D, showRef) {
		Bubble.div.style.display = "block";
		Bubble.div.innerHTML = showRef(D);
		Bubble.getCurPos();
		Bubble.getOffset();
		Bubble.div.style.left = (Bubble.Cursor.x + Bubble.Offset.x) + "px";
		Bubble.div.style.top = (Bubble.Cursor.y + Bubble.Offset.y) + "px";
	},
	hide: function () {
		Bubble.div.innerHTML = "";
		Bubble.div.style.display = "none";
	},
	create: function () {
		Bubble.div = document.createElement("div");
		Bubble.div.id = "BubbleDiv";
		document.getElementsByTagName("body")[0].appendChild(Bubble.div);
		Bubble.div.style.position = 'absolute';
	},
	getOffset: function () {
		var Px = 10, Py = 20;
		var Cx = Bubble.Cursor.x, Cy = Bubble.Cursor.y;
		var w = Bubble.div.clientWidth || 150, h = Bubble.div.clientHeight || 150;
 
		var Ww, Wh, Wx, Wy;
 
		var E = window;
		if (typeof(E.innerWidth) === "number") {
			Ww = E.innerWidth;
			Wh = E.innerHeight;
			Wx = E.pageXOffset;
			Wy = E.pageYOffset;
		} else if (document.documentElement) {
			F = document.documentElement;
			if (F.clientWidth || F.clientHeight) {
				Ww = F.clientWidth;
				Wh = F.clientHeight;
				Wx = F.scrollLeft;
				Wy = F.scrollTop;
			}
		}
 
		// top right
		if ((Cx - Wx) > (Ww / 2) && (Cy - Wy) < h) {
			Px = -w - Px;
		}
		// bottom left
		else if ((Cx - Wx) < (Ww / 2) && (Cy - Wy) > (Wh / 2)) {
			Py = -h - Py;
		}
		// bottom right
		else if ((Cx - Wx) > (Ww / 2) && (Cy - Wy) > (Wh / 2)) {
			Px = -w - Px;
			Py = -h - Py;
		}
 
		Bubble.Offset = {
			x: Px,
			y: Py
		};
	},
	getCurPos: function (C) {
		C = C ? C : window.event;
		if (C && typeof(C.pageX) !== "number" && typeof(C.clientX) === "number") {
			C.pageX = C.clientX;
			C.pageY = C.clientY;
			if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
				C.pageX += document.body.scrollLeft;
				C.pageY += document.body.scrollTop;
			} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				C.pageX += document.documentElement.scrollLeft;
				C.pageY += document.documentElement.scrollTop;
			}
		}
		if(C) {
			Bubble.Cursor = {
				x: C.pageX,
				y: C.pageY
			};
		}
	},
	addEvent: function( obj, type, fn ) {
		if ( window.addEventListener ) {
			obj.addEventListener( type, fn, false );
		} else {
			obj['e'+type+fn] = fn;
			obj[type+fn] = function() { 
				obj['e'+type+fn]( window.event );
			};
			obj.attachEvent( 'on'+type, obj[type+fn] );
		}
	},
	getElementsByClassName: function (searchClass) {
		var classElements = [], i, j, els, pattern;
 
		els = document.getElementsByTagName('*');
		pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
		for (i = 0, j = 0; i < els.length; i += 1) {
			if (pattern.test(els[i].className)) {
				classElements[j] = els[i];
				j += 1;
			}
		}
		return classElements;
	}
};


2. Beispiele


2.1. Thumbshots


JavaScript Code

1
2
3
4
5
<script type="text/javascript">
Bubble.init("externalURL", function (D) {
	return '<img alt="" style="width:150px;height:100px" src="http://www.m-software.de/screenshot/Screenshot.png?url='+ escape(D.href) +'&commingsoonimg=http://fadeout.de/images/thumbshot.png" />';
});
</script>


3. Demo


http://demo.easy-coding.de/javascript/bubble/ und http://demo.easy-coding.de/javascript/bubble/download.zip

Lexikon 4.1.5, developed by www.viecode.com