mirror of
				https://github.com/flynx/ImageGrid.git
				synced 2025-11-04 13:20:10 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			187 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			HTML
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			187 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			HTML
		
	
	
		
			Executable File
		
	
	
	
	
<html>
 | 
						|
 | 
						|
<script src="jquery.js"></script>
 | 
						|
<script>
 | 
						|
 | 
						|
function focus(elem){
 | 
						|
	$('.current.square').removeClass('current')
 | 
						|
	$(elem)
 | 
						|
		.closest('.square')
 | 
						|
			.addClass('current')
 | 
						|
			.trigger('focus')
 | 
						|
}
 | 
						|
 | 
						|
function clickHandler(){
 | 
						|
	focus(this)
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
/* Focus next/prev container element
 | 
						|
* 
 | 
						|
* This will shift focus within a container.
 | 
						|
* 
 | 
						|
* If we reach the start/end of the container, we will "warp" around it 
 | 
						|
* and focus an element from the other end.
 | 
						|
* 
 | 
						|
*/
 | 
						|
// XXX make this customizable (selector-wise)...
 | 
						|
function shiftFocus(n){
 | 
						|
	var cur = $('.current.square')
 | 
						|
	if(n > 0){
 | 
						|
		direction = 'next'
 | 
						|
	} else if(n < 0){
 | 
						|
		direction = 'prev'
 | 
						|
	} else {
 | 
						|
		return cur
 | 
						|
	}
 | 
						|
 | 
						|
	// trigger the event...
 | 
						|
	cur.closest('.container')
 | 
						|
		.trigger('shiftfocus', n)
 | 
						|
 | 
						|
	// XXX will adding a singular [direction] branch make things faster
 | 
						|
	//		for n = +/-1 ???
 | 
						|
	var dir = cur[direction+'All']('.square')
 | 
						|
 | 
						|
	// see if we need to warp arund...
 | 
						|
	if(dir.length < Math.abs(n)){
 | 
						|
		var sq = cur.closest('.container').children('.square')
 | 
						|
		var i = sq.index(cur)
 | 
						|
		return focus($(sq[ n > 0 
 | 
						|
								? (i + n) % sq.length 
 | 
						|
								: sq.length - (Math.abs(i + n) % sq.length) ]))
 | 
						|
	}
 | 
						|
 | 
						|
	// shift the current element...
 | 
						|
	return focus($(dir[Math.abs(n)-1]))
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
// these will roll the container...
 | 
						|
// XXX make this container context-aware...
 | 
						|
function rollContainer(n){
 | 
						|
	var sq = $('.current.square')
 | 
						|
				.closest('.container')
 | 
						|
					.children('.square')
 | 
						|
	// right...
 | 
						|
	if(n > 0){
 | 
						|
		return $(sq.splice(0, n)).appendTo($('.container'))
 | 
						|
	// left...
 | 
						|
	} else if(n < 0){
 | 
						|
		var l = sq.length
 | 
						|
		return $(sq.splice(l+n, l)).prependTo($('.container'))
 | 
						|
	// 0...
 | 
						|
	} else {
 | 
						|
		return
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
// misc demo stuff...
 | 
						|
var UPDATE_ELEMENT_TEXT = true
 | 
						|
function toggleNumberUpdate(){
 | 
						|
	if(UPDATE_ELEMENT_TEXT){
 | 
						|
		UPDATE_ELEMENT_TEXT = false
 | 
						|
	} else {
 | 
						|
		UPDATE_ELEMENT_TEXT = true
 | 
						|
	}
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
// init things...
 | 
						|
$(function(){
 | 
						|
	$('.container')
 | 
						|
		// NOTE: this event will get completed BEFORE the element is focused.
 | 
						|
		.on('shiftfocus', function(evt, n){
 | 
						|
 | 
						|
			// update squares -- numbers...
 | 
						|
			if(UPDATE_ELEMENT_TEXT){
 | 
						|
				var sq = $(this).children('.square')
 | 
						|
				if(n > 0){
 | 
						|
					var s = sq.last().text()*1 + 1
 | 
						|
				} else if(n < 0){
 | 
						|
					var s = sq.first().text()*1 + n
 | 
						|
				} else {
 | 
						|
					return
 | 
						|
				}
 | 
						|
				rollContainer(n).each(function(i, e){
 | 
						|
					$(this).text(i + s)
 | 
						|
				})
 | 
						|
 | 
						|
			// roll as-is...
 | 
						|
			} else {
 | 
						|
				rollContainer(n)
 | 
						|
			}
 | 
						|
		})
 | 
						|
 | 
						|
	$('.square')
 | 
						|
		.click(clickHandler)
 | 
						|
	
 | 
						|
})
 | 
						|
 | 
						|
 | 
						|
</script>
 | 
						|
 | 
						|
<style>
 | 
						|
 | 
						|
.container {
 | 
						|
	position: relative;
 | 
						|
	display: inline-block;
 | 
						|
	height: auto;
 | 
						|
	min-width: 0px;
 | 
						|
	overflow: visible;
 | 
						|
	white-space: nowrap;
 | 
						|
	font-size: 0;
 | 
						|
 | 
						|
	background: gray;
 | 
						|
	padding: 10px;
 | 
						|
}
 | 
						|
 | 
						|
.square {
 | 
						|
	position: relative;
 | 
						|
	display: inline-block;
 | 
						|
	vertical-align: middle;
 | 
						|
	text-align;left;
 | 
						|
	width: 100px;
 | 
						|
	height: 100px;
 | 
						|
	background: silver;
 | 
						|
	font-size: 12pt;
 | 
						|
}
 | 
						|
 | 
						|
.current.square {
 | 
						|
	background: red;
 | 
						|
}
 | 
						|
 | 
						|
</style>
 | 
						|
 | 
						|
<body>
 | 
						|
 | 
						|
 | 
						|
<button onclick="shiftFocus(-5)"><<</button>
 | 
						|
<button onclick="shiftFocus(-1)"><</button>
 | 
						|
<button onclick="shiftFocus(1)">></button>
 | 
						|
<button onclick="shiftFocus(5)">>></button>
 | 
						|
 
 | 
						|
<button onclick="toggleNumberUpdate()">toggle square number update</button>
 | 
						|
 | 
						|
 | 
						|
<div class="meta-container">
 | 
						|
	<div class="container">
 | 
						|
		<div class="square">1</div>
 | 
						|
		<div class="square">2</div>
 | 
						|
		<div class="square current">3</div>
 | 
						|
		<div class="square">4</div>
 | 
						|
		<div class="square">5</div>
 | 
						|
		<div class="square">6</div>
 | 
						|
		<div class="square">7</div>
 | 
						|
	</div>
 | 
						|
</div>
 | 
						|
 | 
						|
 | 
						|
</body>
 | 
						|
<!-- vim:set ts=4 sw=4 : -->
 | 
						|
</html>
 |