I have a divgable div element that uses HTML 5 localStorage to remember its position on the users page. It works great.
I also use window.addEventListener
to update the position if the div was dragged to another open tab. This works, but the div position in one browser tab is slightly offset in the div on another tab. Does anyone know what causes this?
Below is a basic example. Save it as an html file and open it on two different tabs to see what happens.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Untitled Page</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script> <style type="text/css"> .note { position: absolute; width:200px; height:220px; background: #2E2E2E; top: 50px; left: 50px; transform:rotate(7deg); -ms-transform:rotate(7deg); -moz-transform:rotate(7deg); -webkit-transform:rotate(7deg); -o-transform:rotate(7deg); } </style> <script type="text/javascript"> $(function () { var note = $(".note"); updatePosition(note); note.draggable({ stop: function () { var left = $(this).position().left; var top = $(this).position().top; localStorage.setItem("left", left); localStorage.setItem("top", top); } }); window.addEventListener("storage", function (e) { updatePosition(note); }, false); }); function updatePosition(note) { var left = localStorage.getItem("left"); var top = localStorage.getItem("top"); note.css({ left: left + "px", top: top + "px" }); } </script> </head> <body> <div class="note"></div> </body> </html>
UPDATE: CSS transform properties causing an offset.
kmb64 source share