Horizontal scrolling touch menu?
I am building a mobile website, and the client wants a menu bar at the top. The menu is very wide, so he wants it to be horizontally scrolled by dragging it left and right. app.ft.com has similar functionality (although you must view th开发者_如何学编程is on on an iPhone to see it working)
Does anyone know of a jQuery / jqTouch script that can achieve this? I have tried scrollTouch, but that only scrolls the whole page, not just a menu.
I wrote a simple horizontal navigation bar with images for jQuery Mobile that you can scroll by dragging it left or right on mobile devices. This example is very crude but it will give you the general idea. The markup is below:
CSS:
<style type="text/css">
#navBar
{
white-space: nowrap;
height: 55px;
width: 100%;
position: relative;
}
</style>
HTML Markup:
<div id="navBar">
<div style="left: 0%; width: 40%; float: left; display: inline-block; position: absolute; text-align: center;">
<img src="image.png" alt="Nav1" />
<br />
<span style="font-size: 80%">Nav1</span>
</div>
<div style="left: 40%; width: 40%; float: left; display: inline-block; position: absolute; text-align: center;">
<img src="image.png" alt="Nav2" />
<br />
<span style="font-size: 80%">Nav2</span>
</div>
<div style="left: 80%; width: 40%; float: left; display: inline-block; position: absolute; text-align: center;">
<img src="image.png" alt="Nav3" />
<br />
<span style="font-size: 80%">Nav3</span>
</div>
<div style="left: 120%; width: 40%; float: left; display: inline-block; position: absolute; text-align: center;">
<img src="image.png" alt="Nav4" />
<br />
<span style="font-size: 80%">Nav4</span>
</div>
<div style="left: 160%; width: 40%; float: left; display: inline-block; position: absolute; text-align: center;">
<img src="image.png" alt="Nav5" />
<br />
<span style="font-size: 80%">Nav5</span>
</div>
<div style="left: 200%; width: 40%; float: left; display: inline-block; position: absolute; text-align: center;">
<img src="image.png" alt="Nav6" />
<br />
<span style="font-size: 80%">Nav6</span>
</div>
</div>
JavaScript:
<script type="text/javascript" language="javascript">
var bMouseDown = false;
var bMouseUp = true;
var iStartPixelsX = 0;
var iCurrentNavbarPixelsX = 0;
var changePixels = 0;
var leftMostOffsetPixels = 0;
function funcMoveNavBar(pixels) {
$("#navBar").attr("style", "left: " + pixels + "px;");
}
var onOrientationChange = function () {
setTimeout(function () {
funcMoveNavBar(0);
iStartPixelsX = 0;
iCurrentNavbarPixelsX = 0;
changePixels = 0;
leftMostOffsetPixels = $("#navBar div").last().offset().left + $("#navBar div").last().width();
}, 500);
}
$(document).ready(function () {
leftMostOffsetPixels = $("#navBar div").last().offset().left + $("#navBar div").last().width();
if (window.addEventListener) {
window.addEventListener("orientationchange", onOrientationChange, false);
} else if (window.attachEvent) {
window.attachEvent("onorientationchange", onOrientationChange);
}
$("#navBar").bind("vmousedown", function (e) {
bMouseDown = true;
bMouseUp = false;
iStartPixelsX = e.pageX;
});
$("#navBar").bind("vmousemove", function (e) {
if (bMouseDown && !bMouseUp) {
e.preventDefault();
changePixels = (e.pageX - iStartPixelsX) + iCurrentNavbarPixelsX;
funcMoveNavBar(changePixels);
}
});
$("#navBar").bind("vmouseup", function (e) {
bMouseUp = true;
bMouseDown = false;
if (changePixels > 0) {
funcMoveNavBar(0);
changePixels = 0;
iCurrentNavbarPixelsX = 0;
} else if (($("#navBar div").last().offset().left + $("#navBar div").last().width()) < $("#navBar").width()) {
funcMoveNavBar($("#navBar").width() - leftMostOffsetPixels);
iCurrentNavbarPixelsX = $("#navBar").width() - leftMostOffsetPixels;
changePixels = $("#navBar").width() - leftMostOffsetPixels;
} else {
iCurrentNavbarPixelsX = changePixels;
}
});
});
</script>
Keep in mind that $(document).ready() is not recommended when AJAX navigation is enabled in jQuery Mobile, so you might need to tailor this solution to fit your own needs.
A css only solution, utilizing the fact that mobile browsers does'nt create any scrollbars. Some kind of visual guidance and auto-width for the inner one would be good but is not always needed.
JSFiddle to play with http://jsfiddle.net/KaGrc/1
CSS:
#outer {
width: 100%;
overflow: auto;
}
#inner {
width: 800px;
}
HTML:
<div id="outer">
<div id="inner">
content1 content2 content3 content4 content5 content6 content7 content8 content9 content10 content11 content12
</div>
</div>
Try iScroll 4 by cubiq.org. It can be applied to any div on page, horizontal, vertical or both scrolling, have a nice animation (just like on smartphones), is very fast in webkit and Firefox (others are worse) and most of all, is pretty customizable. I spend a 12 hours looking for such scroller and this covers basically all my needs.
Take a look at this
http://plugins.jquery.com/project/Touchwipe-iPhone-iPad-wipe-gesture
精彩评论