jquery - CSS relative slide over absolute to hover over -


trying make game user unable resize/scale screen without getting messed up. i'm working 1 big background , dynamic elements on top of it. elements not scaling (they scaled browser window), wich good, placing them in such position difficult task.

so main goal if user hovers on image (div) div should move x pixels. relative images placed in absolute box (to perform left , top css modification window)

research on net said trying negative margins, didn't seem work, margins indeed going negative, div stays is.

the jsfiddle problem: http://jsfiddle.net/uyx09byn/2/

what's proper way solve this, current jquery code maintain aspect ratio:

$(document).ready(function()  {     setsizes(); });   $(window).resize(function()  {     setsizes(); });  var widthmultiplier; var heightmultiplier;  var normalwidth = 1920; var normalheight = 1080; var offsets; function setsizes() {     widthmultiplier = ($(window).height()/9*16) / normalwidth;     heightmultiplier = $(window).height() / normalheight;      //background image     $('#stadium').height($(window).height());     $('#stadium').width($(window).height()/9*16);      offsets = $('#stadium').offset();      //playerspace     $("#players").width(946 * widthmultiplier);     $("#players").height(130 * heightmultiplier);      $("#players").css("left", (((1920 * widthmultiplier) * 29.8 / 100) + offsets.left) + "px");     $("#players").css("top", (((1080 * heightmultiplier) * 77.9 / 100) + offsets.top) + "px");       //players     $(".player").width(86 * widthmultiplier);     $(".player").height(115 * heightmultiplier);     $(".player").css("margin", (5.5 * heightmultiplier) + "px " + (4 * widthmultiplier) + "px " + (4 * heightmultiplier) + "px " + (4 * widthmultiplier) + "px");      var lefttemp;     $(".player").mouseover(function()     {         console.log("up: " + this.id);         $(this).css({margintop: '-=15px'});     });     $(".player").mouseout(function()     {         console.log("down: " + this.id);         $(this).css({margintop: '+=15px'});     }); }    

here's updated fiddle: https://jsfiddle.net/uyx09byn/17/

a large part of problem lot of you're trying accomplish javascript better done using css.

for example these lines:

$('#stadium').height($(window).height()); $('#stadium').width($(window).height()/9*16); 

can replaced css:

max-width: 1920px; width: calc(100vh / 9 * 16); max-height: 1080px; height: 100vh; 

"100vh" 100% of viewport height.

the scaling effect attempting accomplish javascript better done setting padding-bottom: xx%;on element in question. percentage should set according element's width achieve intended ratio. example, if had 2 elements each taking 50% of parent, padding-bottom set 50% if intended them square.

as moving x pixels on hover, need this:

.player:hover {     backface-visibility: hidden;     transform: translatey(-15px);     transition: .3s; } 

the transform moves element desired direction (in case, 15 pixels y axis), transition makes transform bit smoother, , backface-visibility: hidden;triggers hardware acceleration transform/transition.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -