CrossBrowdy - Examples

Input

Touch gestures

Touch gestures can be managed thanks to the Hammer.js library. Here is an example:

index.html:

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html; charset=utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Input: Touch gestures - Example</title>
		<!-- Loads the needed CSS files: -->
		<link rel="stylesheet" type="text/css" href="main.css" />
		<!-- Loads CrossBrowdy.js (main file): -->
		<script src="CrossBrowdy/CrossBrowdy.js" type="text/javascript" language="javascript"></script><!-- "type" and "language" parameters for legacy clients. -->
		<!-- Loads the other needed script files: -->
		<script src="main.js" type="text/javascript" language="javascript"></script>
	</head>
	<body>
		<!-- Using Hammer.js library internally: https://hammerjs.github.io/ -->
		<div id="container">
			<div id="my_element">Pinch me and rotate me</div>
			You can read more about it in the <a href="https://hammerjs.github.io/" target="_blank">Hammer.js library</a> web site.
		</div>
		<br />
		<!-- The "CB_console" element will be used automatically in the case that the client does not support console: -->
		<div id="CB_console" style="visibility:hidden; overflow:scroll;">
			<span style="font-weight:bold;">Console:</span><br />
		</div>
		<div id="crossbrowdy_info"><a href="/guides#examples" target="_blank">CrossBrowdy.com</a> example</div>
	</body>
</html>

main.css:

body { background-color:#aaddee; word-wrap:break-word; width:100%; height:100%; }
#crossbrowdy_info { position:fixed; bottom:2px; right:2px; }
#crossbrowdy_info a { color:#00aadd; }
#crossbrowdy_info a:hover { color:#0033aa; }
span { color:#aa0000; }
#container { position:absolute; left:0px; top:0px; width:100%; height:100%; background-color:#ddaadd; }
#my_element { position:absolute; background-color:#aa0000; border:2px dotted #0000aa; color:#ccccff; }

main.js:

//Using Hammer.js library internally: https://hammerjs.github.io/


CB_init(main); //It will call the "main" function when ready.


//This function will be called when CrossBrowdy is ready:
function main()
{
	//Managing some touch gestures:
	//* Hammer.js source: https://hammerjs.github.io/ and https://github.com/hammerjs/hammer.js 
	//NOTE: Check Hammer.js documentation for more information.
	
	var HammerJS = CB_Touch.getHammerJSObject();
	if (HammerJS === null) { CB_console("Hammer.js object could not be found!"); return; }
	
	var element = CB_Elements.id("my_element");
	if (element === null) { CB_console("The 'my_element' element could not be found!"); return; }
	
	CB_console("Setting touch gestures for 'my_element' using Hammer.js library...");
	
	element.style.width = (CB_Screen.getWindowWidth() / 4) + "px";
	element.style.height = (CB_Screen.getWindowHeight() / 4) + "px";
	element.style.left = (parseInt(element.style.width) * 1.5) + "px";
	element.style.top = (parseInt(element.style.height) * 1.5) + "px";

	var mananger = new HammerJS.Manager(CB_Elements.id("container") || document.body);
	
	var pinch = new HammerJS.Pinch();
	var rotate = new HammerJS.Rotate();
	pinch.recognizeWith(rotate);
	
	mananger.add([pinch, rotate]);
	
	//Function when rotating:
	mananger.on
	(
		"rotate",
		function(e)
		{
			element.style.transform = "rotate(" + Math.round(e.rotation) + "deg)";
		}
	);
	
	//Function when pinching:
	mananger.on
	(
		"pinchin pinchout",
		function(e)
		{
			var elementWidth = parseInt(element.style.width);
			var elementHeight = parseInt(element.style.height);
			element.style.backgroundColor = (element.style.backgroundColor === "orange") ? "purple" : "orange";
			if (e.type === "pinchout" && elementWidth < 500 && elementHeight < 500)
			{
				element.style.width = (elementWidth + (e.distance * 0.05)) + "px";
				element.style.height = (elementHeight + (e.distance * 0.05)) + "px";
			}
			else //"pinchin":
			{
				if (elementWidth > 50 && elementHeight > 50)
				{
					element.style.width = (elementWidth - (e.distance * 0.05)) + "px";
					element.style.height = (elementHeight - (e.distance * 0.05)) + "px";
				}
			}
		}
	);
}

Try this example

Read the documentation of the Hammer.js library to get more information about how to use it.

You can check the Guides & Tutorials category as well as the API documentation in the case you need more information.

Go back to Guides & Tutorials

« PrevReturnNext »
CrossBrowdy by Joan Alba Maldonado