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
| <!doctype html>
<html lang="fr">
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta charset="utf-8">
<meta name="Author" content="Daniel Hagnoul">
<title>Forum jQuery</title>
<style>
body { background-color:#dcdcdc; color:#000000; font-family:sans-serif; font-size:medium; font-style:normal; font-weight:normal; line-height:normal; letter-spacing:normal; }
h1,h2,h3,h4,h5 { font-family:serif; }
div,p,h1,h2,h3,h4,h5,h6,ul,ol,dl,form,table,img { margin:0px; padding:0px; }
img {border:none; }
h1 { font-size:2em; text-shadow: 4px 4px 4px #bbbbbb; text-align:center; }
p { padding:6px; }
ul,ol,dl {list-style:none; padding-left:6px; padding-top:6px; }
li {padding-bottom:6px; }
.conteneur { width:95%; min-width:800px; min-height:500px; margin:12px auto; background-color:#FFFFFF; color:#000000; border:1px solid #666666; }
/* TEST */
#btnRotate {margin:12px; }
.divTest {width:400px; height:150px; margin:12px; padding:6px; border:1px solid grey; background-color:lightgreen; }
.result {margin-top:120px; }
</style>
</head>
<body>
<h1>Forum jQuery</h1>
<section class="conteneur">
<button id="btnRotate">Tourne de 45 degrés !</button>
<div class="divTest">
<p>Un mot pour remplir</p>
</div>
<div class="result"></div>
</section>
<script charset="utf-8" src="http://code.jquery.com/jquery-1.5.1.min.js"></script>
<script>
(function($){
var _Options = {
degrees: 0,
debug: true
};
var _avecStyle = function(self, matrix){
self.attr("style", "-moz-transform:" + matrix +
"; -webkit-transform:" + matrix +
"; -ms-transform:" + matrix +
"; -o-transform:" + matrix +
"; transform:" + matrix +
";");
};
var _setDegrees = function(self, value){
var v = parseInt(value, 10) || 0;
if (v != _Options.degrees){
_Options.degrees = v;
var rad = v * Math.PI * 2.0 / 360.0,
costheta = Math.cos(rad),
sintheta = Math.sin(rad),
a = parseFloat(costheta, 10).toFixed(8),
c = parseFloat(-sintheta, 10).toFixed(8),
b = parseFloat(sintheta, 10).toFixed(8),
d = parseFloat(costheta, 10).toFixed(8),
matrix = "matrix(" + a + ", " + b + ", " + c + ", " + d + ", 0, 0);";
_avecStyle(self, matrix);
var optionsChangedEvent = new $.Event("optionsChanged");
optionsChangedEvent.dvjh = {
initiateur: self,
optionsKey: "degrees",
optionsValue: v
};
self.trigger(optionsChangedEvent);
}
};
$.fn.extend({
plugin: function(options){
var self = this;
if (_Options.debug){
self.bind("optionsChanged", function(event){
var obj = event.dvjh,
el = obj.initiateur[0];
console.log("L'option " +
obj.optionsKey +
" a pris la valeur " +
obj.optionsValue +
" le " +
new Date(event.timeStamp).toLocaleString() +
" a la demande de l'élément " +
el.tagName +
" , ID = " +
el.id +
" , Class = " +
el.className);
});
}
if (options != undefined){
$.each(options, function(key, value){
self.pluginSetOptions(key, value);
});
} else {
// donner une valeur par défaut
self.pluginSetOptions("degrees", "45");
}
return this; // Indispensable !
},
pluginSetOptions: function(key, value){
switch(key){
case "degrees":
_setDegrees(this, value);
break;
default:
throw "L'option " + key + " n'existe pas";
};
},
pluginGetOptions: function(key){
switch(key){
case "degrees":
return _Options.degrees;
break;
default:
throw "L'option " + key + " n'existe pas";
};
}
});
jQuery.fn.dvjhRotate = function(options){
return $(this).plugin(options);
};
})(jQuery.sub());
$(function() {
$("div.result").append("top = " + $("div.divTest").offset().top, ", left = " + $("div.divTest").offset().left, ", height = " + $("div.divTest").height(), ", width = " + $("div.divTest").width(), " ; ");
$("#btnRotate").click(function(){
var objsRotate = $("div.divTest").dvjhRotate();
$("div.result").append("top = " + $("div.divTest").offset().top, ", left = " + $("div.divTest").offset().left, ", height = " + $("div.divTest").height(), ", width = " + $("div.divTest").width());
// objsRotate.pluginSetOptions("degrees", "-105");
});
});
</script>
</body>
</html> |
Partager