Jquery slider and draggable dont work -
i'm making banner can resize , drag image. works fine in jsfiddle. reason dont work on webpage.
here example in jsfiddle:
http://jsfiddle.net/dennisbetman/tnaga/
and here how looks on webpage:
http://jewelbeast.com/posts/imgreplace.html
so if can see. slider dont work. , image draggable doing weird to.
i called script jquery , jquery ui in head.
he code used webpage.
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>jquery ui draggable - default functionality</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" /> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <style> div { width:370px; height:204px; position:relative; overflow:hidden; border-top-left-radius: 7px; border-top-right-radius: 7px; } #box{ background:black; cursor: move; border: 1px solid black; } #hidden-img{ display:none; } </style> </head> <body> <div id="box"> <img src="http://www.hdwallpapersarena.com/wp-content/uploads/2013/04/high-resolution-fantasy-woman.jpg" id="hidden-img" /> <img src="http://www.hdwallpapersarena.com/wp-content/uploads/2013/04/high-resolution-fantasy-woman.jpg" id="img" width="371" height="auto" style="top:-0px; left:-0px;" /> </div> <!-- style="top:-262px; left:-425px;" --> <div id="zoom"></div> <input type="hidden" id="amount-width" style="border: 0; color: #f6931f; font-weight: bold;" /> <input type="hidden" id="amount-height" style="border: 0; color: #f6931f; font-weight: bold;" /> <div class="text"></div> <p> position x: <input type="text" id="val-x" style="border:0; color:#f6931f; font-weight:bold;" /> </p> <p> position y: <input type="text" id="val-y" style="border:0; color:#f6931f; font-weight:bold;" /> </p> <p> <input type="file" onchange="readurl(this);" /> </p> <script> var previousvalue = 0; var hiddenimagewidth = $("#hidden-img").width() - 370; jquery("#zoom").slider({ max: hiddenimagewidth, slide: function(event, ui){ var slidervalue = jquery("#zoom").slider("value"); jquery("#img").width(370 + slidervalue); var pos = $("#img").position(); // returns object attribute top , left var top = pos.top; // top offset position var left = pos.left; // left offset position $("#val-x").val(left); $("#val-y").val(top); if (left > 0){ $("#img").css("left",'0px'); } if (top > 0){ $("#img").css("top",'0px'); } $("#amount-width").val(jquery("#img").css("width")); $("#amount-height").val(jquery("#img").css("height")); var width = $("#img").width(); var widthzoom = width + slidervalue; var widthverschil = widthzoom - slidervalue; var totalwidth = widthverschil - '370'; var height = $("#img").height(); var totalheight = height - '207'; if (slidervalue < previousvalue){ var t, l; t = $('#img').position().top; l = $('#img').position().left; if(t < 0) t = t + (previousvalue - slidervalue); if(l < 0) l = l + (previousvalue - slidervalue); $('#img').offset({top: t, left: l}); } previousvalue = slidervalue; $("#img").draggable({ containment: [-totalwidth, -totalheight, 0, 0], scroll: false, iframefix: true, }); $('.text').html('<br/>the file size = ' + height + ' x ' + widthverschil); } }); var height = $("#img").height(); var totalheight = height - '207'; $("#img").draggable ({ containment: [0, -totalheight, 0, 0], snap: false, drag: function(event, ui) { $("#val-x").val(ui.position.left); $("#val-y").val(ui.position.top); } }); $("#img").offset({left: $(this).attr('position')}); function readurl(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $('#img') .attr('src', e.target.result) }; reader.readasdataurl(input.files[0]); } } </script> </body> </html>
you can see script making al happening on end of body. tried put in head nothing work, wont see slider. ect.
i tried place scripts
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
under script function. wont work either.
i hope see problem.
thanks.
you have wrap jquery's code inside document ready function:
var previousvalue = 0; var hiddenimagewidth = $("#hidden-img").width() - 370; $(document).ready(function () { jquery("#zoom").slider({ max: hiddenimagewidth, slide: function (event, ui) { var slidervalue = jquery("#zoom").slider("value"); jquery("#img").width(370 + slidervalue); var pos = $("#img").position(); // returns object attribute top , left var top = pos.top; // top offset position var left = pos.left; // left offset position $("#val-x").val(left); $("#val-y").val(top); if (left > 0) { $("#img").css("left", '0px'); } if (top > 0) { $("#img").css("top", '0px'); } $("#amount-width").val(jquery("#img").css("width")); $("#amount-height").val(jquery("#img").css("height")); var width = $("#img").width(); var widthzoom = width + slidervalue; var widthverschil = widthzoom - slidervalue; var totalwidth = widthverschil - '370'; var height = $("#img").height(); var totalheight = height - '207'; if (slidervalue < previousvalue) { var t, l; t = $('#img').position().top; l = $('#img').position().left; if (t < 0) t = t + (previousvalue - slidervalue); if (l < 0) l = l + (previousvalue - slidervalue); $('#img').offset({ top: t, left: l }); } previousvalue = slidervalue; $("#img").draggable({ containment: [-totalwidth, -totalheight, 0, 0], scroll: false, iframefix: true, }); $('.text').html('<br/>the file size = ' + height + ' x ' + widthverschil); } }); var height = $("#img").height(); var totalheight = height - '207'; $("#img").draggable({ containment: [0, -totalheight, 0, 0], snap: false, drag: function (event, ui) { $("#val-x").val(ui.position.left); $("#val-y").val(ui.position.top); } }); $("#img").offset({ left: $(this).attr('position') }); }); function readurl(input) { if (input.files && input.files[0]) { var reader = new filereader(); reader.onload = function (e) { $('#img') .attr('src', e.target.result) }; reader.readasdataurl(input.files[0]); } }
Comments
Post a Comment