javascript - HTML - Body Rotation Affecting Fixed Elements -
i have javascript function used shake screen second.
function shakescreen() { var degrees = 0; var goal = 0; var goals = [5,-5,4,-4,3,-4,2,-2,1,-1,0]; function frame() { if(goals[goal] < 0) { degrees -= 1; } else { degrees += 1; } get('body','tag'); current[0].style.transform = 'rotate(' + degrees + 'deg)'; if(degrees === goals[goal]) { goal += 1; if(goal === goals.length) { clearinterval(intervalid); } } } var intervalid = setinterval(frame,16); } inside html body, first element has fixed position. element pop-up displays messagers users, , has fixed position appears in same position on screen if end user has scrolled down or page. here snippet of html:
<body> <div id='fade' onclick="fade('out')"><div id='alert'></div></div> <!-- more unrelated content --> here css specified element above:
#fade { z-index: 2750; position: fixed; /*important*/ display: none; width: 100%; height: 100%; margin: 0; padding: 0; background-color: rgba(0, 0, 0, 0); text-align: center; } #fade:hover { cursor: pointer; } when javascript function shakescreen() executed, fade element gets misaligned , moves top of body rather remaining in center of end user's screen. here jsfiddle: https://jsfiddle.net/turkey3/ug9zx1d2/3/ scroll down halfway , you'll see button. click button shake screen. you'll notice split second fade element starts appearing @ center of screen, moves top after starts shaking. also, see how want be, comment out shakescreen function that's inside setup function , run code.
edit: fade element dark background. alert element white box message shows user.
solution
i found out best method was, rather rotating body element, wrap other code insider div , shake division rather entire body. after that, alert message showed in center of screen. here fixed code: https://jsfiddle.net/turkey3/ug9zx1d2/4/
here html wrapped in div:
<body> <div id='fade' onclick="fade('out')"><div id='alert'></div></div> <div id='shake'> <!-- more unrelated content --> </div> and updated js:
function shakescreen() { var degrees = 0; var goal = 0; var goals = [5,-5,4,-4,3,-4,2,-2,1,-1,0]; function frame() { if(goals[goal] < 0) { degrees -= 1; } else { degrees += 1; } get('shake','id'); current.style.transform = 'rotate(' + degrees + 'deg)'; if(degrees === goals[goal]) { goal += 1; if(goal === goals.length) { clearinterval(intervalid); } } } var intervalid = setinterval(frame,16); }
Comments
Post a Comment