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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
| <!DOCTYPE html>
<html lang="fr" dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0">
<meta name="author" content="Daniel Hagnoul">
<title>Forum jQuery</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/headjs/0.99/head.min.js"></script>
<script>
"use strict";
head.js(
"http://d3js.org/d3.v3.min.js",
"http://code.jquery.com/jquery-2.0.3.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/i18n/jquery-ui-i18n.min.js",
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
"http://danielhagnoul.developpez.com/lib/dvjh/d3Base.js", function(){
// Plug-in Sidr.
;(function( $ ){
var sidrMoving = false,
sidrOpened = false;
// Private methods
var privateMethods = {
// Check for valids urls
// From : http://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-an-url
isUrl: function (str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
if(!pattern.test(str)) {
return false;
} else {
return true;
}
},
// Loads the content into the menu bar
loadContent: function($menu, content) {
$menu.html(content);
},
// Add sidr prefixes
addPrefix: function($element) {
var elementId = $element.attr('id'),
elementClass = $element.attr('class');
if(typeof elementId === 'string' && '' !== elementId) {
$element.attr('id', elementId.replace(/([A-Za-z0-9_.\-]+)/g, 'sidr-id-$1'));
}
if(typeof elementClass === 'string' && '' !== elementClass && 'sidr-inner' !== elementClass) {
$element.attr('class', elementClass.replace(/([A-Za-z0-9_.\-]+)/g, 'sidr-class-$1'));
}
$element.removeAttr('style');
},
execute: function(action, name, callback) {
// Check arguments
if(typeof name === 'function') {
callback = name;
name = 'sidr';
}
else if(!name) {
name = 'sidr';
}
// Declaring
var $menu = $('#' + name),
$body = $($menu.data('body')),
$html = $('html'),
menuWidth = $menu.outerWidth(true),
speed = $menu.data('speed'),
side = $menu.data('side'),
bodyAnimation,
menuAnimation,
scrollTop;
// Open Sidr
if('open' === action || ('toogle' === action && !$menu.is(':visible'))) {
// Check if we can open it
if( $menu.is(':visible') || sidrMoving ) {
return;
}
// If another menu opened close first
if(sidrOpened !== false) {
methods.close(sidrOpened, function() {
methods.open(name);
});
return;
}
// Lock sidr
sidrMoving = true;
// Left or right?
if(side === 'left') {
bodyAnimation = {left: menuWidth + 'px'};
menuAnimation = {left: '0px'};
}
else {
bodyAnimation = {right: menuWidth + 'px'};
menuAnimation = {right: '0px'};
}
// Prepare page
scrollTop = $html.scrollTop();
//$html.css('overflow-x', 'hidden').scrollTop(scrollTop);
// Open menu
$body.css({
width: $body.width(),
position: 'absolute'
}).animate(bodyAnimation, speed);
$menu.css('display', 'block').animate(menuAnimation, speed, function() {
sidrMoving = false;
sidrOpened = name;
// Callback
if(typeof callback === 'function') {
callback(name);
}
});
}
// Close Sidr
else {
// Check if we can close it
if( !$menu.is(':visible') || sidrMoving ) {
return;
}
// Lock sidr
sidrMoving = true;
// Right or left menu?
if(side === 'left') {
bodyAnimation = {left: 0};
menuAnimation = {left: '-' + menuWidth + 'px'};
}
else {
bodyAnimation = {right: 0};
menuAnimation = {right: '-' + menuWidth + 'px'};
}
// Close menu
scrollTop = $html.scrollTop();
$html.removeAttr('style').scrollTop(scrollTop);
$body.animate(bodyAnimation, speed);
$menu.animate(menuAnimation, speed, function() {
$menu.removeAttr('style');
$body.removeAttr('style');
$('html').removeAttr('style');
sidrMoving = false;
sidrOpened = false;
// Callback
if(typeof callback === 'function') {
callback(name);
}
});
}
}
};
// Sidr public methods
var methods = {
open: function(name, callback) {
privateMethods.execute('open', name, callback);
},
close: function(name, callback) {
privateMethods.execute('close', name, callback);
},
toogle: function(name, callback) {
privateMethods.execute('toogle', name, callback);
}
};
$.sidr = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'function' || typeof method === 'string' || ! method ) {
return methods.toogle.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.sidr' );
}
};
$.fn.sidr = function( options ) {
var settings = $.extend( {
name : 'sidr', // Name for the 'sidr'
speed : 200, // Accepts standard jQuery effects speeds (i.e. fast, normal or milliseconds)
side : 'left', // Accepts 'left' or 'right'
source : null, // Override the source of the content.
renaming : true, // The ids and classes will be prepended with a prefix when loading existent content
body : 'body' // Page container selector,
}, options);
var name = settings.name,
$sideMenu = $('#' + name);
// If the side menu do not exist create it
if( $sideMenu.length === 0 ) {
$sideMenu = $('<div />')
.attr('id', name)
.appendTo($('body'));
}
// Adding styles and options
$sideMenu
.addClass('sidr')
.addClass(settings.side)
.data({
speed : settings.speed,
side : settings.side,
body : settings.body
});
// The menu content
if(typeof settings.source === 'function') {
var newContent = settings.source(name);
privateMethods.loadContent($sideMenu, newContent);
}
else if(typeof settings.source === 'string' && privateMethods.isUrl(settings.source)) {
$.get(settings.source, function(data) {
privateMethods.loadContent($sideMenu, data);
});
}
else if(typeof settings.source === 'string') {
var htmlContent = '',
selectors = settings.source.split(',');
$.each(selectors, function(index, element) {
htmlContent += '<div class="sidr-inner">' + $(element).html() + '</div>';
});
// Renaming ids and classes
if(settings.renaming) {
var $htmlContent = $('<div />').html(htmlContent);
$htmlContent.find('*').each(function(index, element) {
var $element = $(element);
privateMethods.addPrefix($element);
});
htmlContent = $htmlContent.html();
}
privateMethods.loadContent($sideMenu, htmlContent);
}
else if(settings.source !== null) {
$.error('Invalid Sidr Source');
}
return this.each(function(){
var $this = $(this),
data = $this.data('sidr');
// If the plugin hasn't been initialized yet
if ( ! data ) {
$this.data('sidr', name);
$this.click(function(e) {
e.preventDefault();
methods.toogle(name);
});
/*
* Daniel Hagnoul
* 2013-08-22
*
* Modification du plugin pour fermer
* le menu MOBILE lorsque la largeur
* disponible est suffisante.
*/
$( window ).on( "resize", function(){
if ( this.outerWidth > 799 ){
methods.close( name );
}
});
}
});
};
})( jQuery );
$( function(){
$('#button').sidr();
});
$( window ).load( function(){
});
});
</script>
<link href='http://fonts.googleapis.com/css?family=Sofia|Ubuntu:400|Kreon'>
<link rel="stylesheet" href="http://danielhagnoul.developpez.com/styles/dvjhRemBase.css">
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/themes/sunny/jquery-ui.min.css">
<style>
/* TEST -- Nota bene : ici 1 rem est égal à 1 px, voir dvjhRemBase.css */
:focus {
outline:none;
border:0;
}
ul {
list-style:none;
list-style-image:none;
margin:0;
padding:0;
}
.sidr ul li a {
color:#999;
display:block;
transition:color .2s eas-ine-out;
}
@media screen and (min-width: 800px) {
#button {
display:none;
}
.sidr .nav-logo {
width:120px;
}
.sidr ul {
position:relative;
width:100%;
text-align:center;
z-index:99;
}
.sidr ul li {
position:static;
display:inline-block;
text-align:center;
*display:inline;
*zoom:1;
}
.sidr ul li a {
padding:30px 15px;
}
.sidr ul ul.sub-menu {
position:absolute;
width:100%;
top:100%;
left:0;
z-index:999;
background:black;
z-index:1000;
opacity:0;
padding:15px 0;
visibility:hidden;
transition:opacity .1s ease-in-out, visibility .1s ease-in-out;
}
.sidr ul li:hover ul.sub-menu {
opacity:1;
visibility:visible;
}
.sidr ul li a:hover {
color:#9C3;
}
}
@media screen and (max-width: 800px) {
#button {
display:block;
width:100%;
padding:30px 0;
background:black;
z-index:999;
color:#fff;
}
.sidr .nav-logo {
display:none;
}
.sidr {
display: none;
position:absolute;
position: fixed;
top: 0;
height: 100%;
z-index: 999;
width: 260px;
overflow-x: none;
overflow-y: auto;
background:black;
}
.sidr.right {
left: auto;
right: -260px;
}
.sidr.left {
left: -260px;
right: auto;
}
.sidr ul {
display: block;
}
.sidr ul li {
display: block;
}
.sidr ul li a {
padding:10px 15px;
}
.sidr ul li a:active {
color:#9C3;
}
}
[class^="icon-"]:before, [class*=" icon-"]:before, [class^="icon-"]:after, [class*=" icon-"]:after{
display:inline-block;
}
</style>
</head>
<body>
<header>
<hgroup>
<h1>Forum jQuery</h1>
<h2>
<a href="">Lien</a>
</h2>
</hgroup>
</header>
<nav id="sidr">
<ul class="nav">
<li><a href="#">Menu 1</a></li>
<li><a href="#">Menu 2</a>
<ul class="sub-menu">
<li><a href="#">Sub Menu 1</a></li>
<li><a href="#">Sub Menu 2</a></li>
<li><a href="#">Sub Menu 3</a></li>
<li><a href="#">Sub Menu 4</a></li>
<li><a href="#">Sub Menu 5</a></li>
</ul>
</li>
<li><a href="#">Menu 3</a></li>
<li><a href="#">Menu 4</a></li>
<li><a href="#">Menu 5</a></li>
<li><a href="#">Menu 6</a></li>
</ul>
</nav>
<section class="conteneur">
<a id="button" href="#sidr">Open</a>
</section>
<footer itemscope itemtype="http://danielhagnoul.developpez.com/">
<time datetime="2013-08-22T10:03:02.849+02:00" pubdate>2013-08-22T10:03:02.849+02:00</time>
<span itemprop="name">Daniel Hagnoul</span>
<a href="http://www.developpez.net/forums/u285162/danielhagnoul/" itemprop="url">@danielhagnoul</a>
<a href="http://danielhagnoul.developpez.com/" itemprop="url">Mon cahier dexercices</a>
<a href="http://javascript.developpez.com/faq/jquery/" itemprop="url">FAQ</a>
<a href="http://javascript.developpez.com/cours/?page=frameworks#jquery" itemprop="url">Tutoriels</a>
</footer>
</body>
</html> |
Partager