var _this; var Game = { initElements : function(parts, placements){ _this = this; parts.each(function(index, value){ $.data($(this)[0], 'pos', { top : $(this).css('top'), left : $(this).css('left') }); $(this).data('isOut', '1'); _this.createDraggable($(this)); }); placements.each(function(index, value){ _this.createDroppable($(this)); }); }, createDraggable : function(draggable){ draggable.draggable({ containment : 'parent', stack : 'img.img-parts', stop : function(event,ui){ if ($(this).data('isOut') == 1){ var objstyle = { top : $(this).data('pos').top, left : $(this).data('pos').left } $(this).animate(objstyle); } } }); }, createDroppable : function(droppable){ droppable.droppable({ tolerance : 'touch', drop : function(event, ui){ if (ui.draggable.data('name') == $(this).data('drop')){ ui.draggable.data('isOut', '0'); var objstyle = { top : $(this).css('top'), left : $(this).css('left') } ui.draggable.animate(objstyle); } else ui.draggable.data('isOut', '1'); }, out : function (event,ui){ ui.draggable.data('isOut', '1'); } }); } } function appendBackLink(){ var backlink = 'BACK TO PREVIOUS PAGE'; var delay = null; if($(window).width() < 800){ $('body').append(backlink); $('#back-link').css({fontFamily: 'sans-serif' , color: '#0769AD', fontWeight:900, display:'block', marginTop:'15px'}); } clearTimeout(delay); $(window).on('resize',function(){ delay = setTimeout(function(){ if($(this).width()<800){ if($('#back-link').length==0){ $('body').append(backlink); $('#back-link').css({fontFamily: 'sans-serif' , color: '#0769AD', fontWeight:900, display:'block', marginTop:'15px'}); } } else{ if($('#back-link').length!=0) $('#back-link').remove(); } },500) }); } $(document).ready(function() { appendBackLink(); Game.initElements( $('#gamecontainer img[data-name]'), $('#gamecontainer div[data-drop]')); });