Skip to content

Commit e0deb40

Browse files
author
John Bai
committed
Changing how to use the plugin. The method will now be invoked on the element that will become the miniview. An element will be passed which will act as the source parent element for the map.
1 parent e894e04 commit e0deb40

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# jquery-minimap
2-
A jQuery plugin that creates an interactive minimap of a parent element and its children.
2+
A jQuery plugin that creates an interactive minimap of an element and its children.
33

44
##Demo
55
https://john-bai.github.io/jquery-minimap
66

77
##Usage
88
```javascript
9-
$parent.minimap( $children );
9+
$( "#minimap" ).minimap( $element );
1010
```

demo.html

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@
3737
z-index: 1;
3838
}
3939

40-
.mini {
40+
.minimap-node {
4141
position: absolute;
4242
background-color: rgb(96, 122, 134);
4343
}
4444

45-
.port {
45+
.minimap-viewport {
4646
position: absolute;
4747
box-sizing: border-box;
4848
background-color: rgba(79, 111, 126, 0.4);
@@ -57,10 +57,11 @@
5757
<div id="parent"></div>
5858

5959
<script>
60-
$( function () {
61-
var $parent = $( "#parent" );
60+
$(function () {
61+
// First, lets build an interesting example of parent and child DOM elements...
6262

6363
// Add children to the parent.
64+
var $parent = $( "#parent" );
6465
var childrenFragment = document.createDocumentFragment();
6566
var left = 0;
6667
var top = 0;
@@ -82,9 +83,11 @@
8283
} );
8384
} );
8485

85-
// Initialize the minimap plugin.
86-
$parent.minimap( $children );
87-
} );
86+
87+
// Now invoke the minimap method on the element you'd like to become the minimap,
88+
// passing a reference to the element you'd like the map to be based on.
89+
$( "#minimap" ).minimap( $parent );
90+
});
8891
</script>
8992
</body>
9093
</html>

jquery-minimap.js

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
(function( $ ) {
2-
$.fn.minimap = function( children ) {
2+
$.fn.minimap = function( $mapSource ) {
33
var x, y, l, t, w, h;
4-
var $parent = this;
54
var $window = $( window );
6-
var $minimap = $( "#minimap" );
5+
var $minimap = this;
76
var minimapWidth = $minimap.width();
87
var minimapHeight = $minimap.height();
9-
var $viewport = $( "<div></div>" ).addClass( "port" );
8+
var $viewport = $( "<div></div>" ).addClass( "minimap-viewport" );
109
$minimap.append( $viewport );
1110
synchronize();
1211

1312
$window.on( "resize", synchronize );
14-
$parent.on( "scroll", synchronize );
15-
$parent.on( "drag", init );
13+
$mapSource.on( "scroll", synchronize );
14+
$mapSource.on( "drag", init );
1615
$minimap.on( "mousedown touchstart", down );
1716

1817
function down( e ) {
@@ -67,13 +66,13 @@
6766
l += dx;
6867
t += dy;
6968

70-
var coefX = minimapWidth / $parent[ 0 ].scrollWidth;
71-
var coefY = minimapHeight / $parent[ 0 ].scrollHeight;
69+
var coefX = minimapWidth / $mapSource[ 0 ].scrollWidth;
70+
var coefY = minimapHeight / $mapSource[ 0 ].scrollHeight;
7271
var left = l / coefX;
7372
var top = t / coefY;
7473

75-
$parent[ 0 ].scrollLeft = Math.round( left );
76-
$parent[ 0 ].scrollTop = Math.round( top );
74+
$mapSource[ 0 ].scrollLeft = Math.round( left );
75+
$mapSource[ 0 ].scrollTop = Math.round( top );
7776

7877
redraw();
7978
}
@@ -83,10 +82,10 @@
8382
}
8483

8584
function synchronize() {
86-
var dims = [ $parent.width(), $parent.height() ];
87-
var scroll = [ $parent.scrollLeft(), $parent.scrollTop() ];
88-
var scaleX = minimapWidth / $parent[ 0 ].scrollWidth;
89-
var scaleY = minimapHeight / $parent[ 0 ].scrollHeight;
85+
var dims = [ $mapSource.width(), $mapSource.height() ];
86+
var scroll = [ $mapSource.scrollLeft(), $mapSource.scrollTop() ];
87+
var scaleX = minimapWidth / $mapSource[ 0 ].scrollWidth;
88+
var scaleY = minimapHeight / $mapSource[ 0 ].scrollHeight;
9089

9190
var lW = dims[ 0 ] * scaleX;
9291
var lH = dims[ 1 ] * scaleY;
@@ -111,19 +110,19 @@
111110
}
112111

113112
function init() {
114-
$minimap.find( ".mini" ).remove();
113+
$minimap.find( ".minimap-node" ).remove();
115114
//creating mini version of the supplied children
116-
children.each( function() {
115+
$mapSource.children().each( function() {
117116
var $child = $( this );
118-
var mini = $( "<div></div>" ).addClass( "mini" );
117+
var mini = $( "<div></div>" ).addClass( "minimap-node" );
119118
$minimap.append( mini );
120-
var ratioX = minimapWidth / $parent[ 0 ].scrollWidth;
121-
var ratioY = minimapHeight / $parent[ 0 ].scrollHeight;
119+
var ratioX = minimapWidth / $mapSource[ 0 ].scrollWidth;
120+
var ratioY = minimapHeight / $mapSource[ 0 ].scrollHeight;
122121

123122
var wM = $child.width() * ratioX;
124123
var hM = $child.height() * ratioY;
125-
var xM = ($child.position().left + $parent.scrollLeft()) * ratioX;
126-
var yM = ($child.position().top + $parent.scrollTop()) * ratioY;
124+
var xM = ($child.position().left + $mapSource.scrollLeft()) * ratioX;
125+
var yM = ($child.position().top + $mapSource.scrollTop()) * ratioY;
127126

128127
mini.css( {
129128
width : Math.round( wM ),

0 commit comments

Comments
 (0)