Here is my JSFiddle.
What is the problem?
I have two
<div>
s i.e with red and blue classes. red is just to indicate original position of blue class. Rest everything, we are only focusing on blue.Now, when I
hover
on blue, it starts rotating continuously till infinity. And when it rotates more than 95deg approximately, we can see that some part of blue class gets out of the left of the body. Similarly, when it reaches on the upper-half during rotation, it's some part gets out on the upper side of the body.
What I want:
I want is that when the blue starts hiding to the left, it should be pushed from left so as to get shifted to the right and thus avoiding from getting hidden. Similarly, it should be pushed from the top gradually as it starts hiding on the upper side.
You can apply
margin
or eventranslate
for changing the position of the blue. But, position absolute is not allowed in this case. So, I have to stick at that. Sorry about that.
I have got exhausted knowing what I have been doing wrong. It would be a great help if you help me find out. Thanks in advance.
[Note: I can't change the origin of rotation. It has to be top-left of the body forcing blue from hiding.]
$(function(){
var blueDivOffset = $(".blue").offset();
var blueDivOffsetTop = blueDivOffset.top; //20
var blueDivOffsetLeft = blueDivOffset.left; //28
function rotate() {
//console.log(blueDivOffsetTop);
//console.log(blueDivOffsetLeft);
if( 0 < blueDivOffsetTop < 20 ) {
var operation = 20 - blueDivOffsetTop;
$(".blue").css({"margin-top": operation+"px"});
} else if (blueDivOffsetTop < 0){
var operation = 20 + (-1) * blueDivOffsetTop;
$(".blue").css({"margin-top": operation+"px"});
} else {
$(".blue").css({"margin-top": 0});
}
if( 0 < blueDivOffsetLeft < 28 ) {
var operation = 28 - blueDivOffsetLeft;
$(".blue").css({"margin-left": operation+"px"});
//console.log("hi");
} else if (blueDivOffsetLeft < 0){
var operation = 28 + (-1) * blueDivOffsetLeft;
$(".blue").css({"margin-left": operation+"px"});
} else {
$(".blue").css({"margin-left": 0});
}
var margLeft = $(".blue").css("margin-left");
console.log(margLeft);
}
$(".blue").hover(function(e){
to = window.setInterval(rotate, 1);
},function(e) {
window.clearInterval(to);
});
//setInterval(rotate, 1);
});
* {
box-sizing: border-box;
}
div > div {
height: 50px;
width: 300px;
}
.main {
margin: 20px;
position: relative;
}
.blueParent {
}
.blue {
background-color: blue;
/* position: absolute;
top: 0;
left: 0;
transform: rotate(180deg) translate(0,0);
transform-origin: 25px 25px; */
background-color: blue;
transform: rotate(0deg) translate(0,-50px);
transform-origin: 25px -25px;
}
.blue:hover {
animation: rotate 5s linear infinite reverse;
}
.red {
background-color: red;
}
@keyframes rotate {
0 { transform: rotate(-90deg) translate(0,-50px); }
25% { transform: rotate(-180deg) translate(0,-50px); }
50% { transform: rotate(-270deg) translate(0,-50px); }
100% { transform: rotate(-360deg) translate(0,-50px); }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<div class="red"></div>
<div class="blue" id="blue"></div>
</div>