jquery rotation not working in ie - javascript

Hi guys i got some code which is suppose to rotate a div and make it move up. Its working in all browsers apart from IE. Any help would be great.
function rotate(degree) {
$elie.css({
'-webkit-transform': 'rotate(' + degree + 'deg)',
'-moz-transform': 'rotate(' + degree + 'deg)',
'-o-transform': 'rotate(' + degree + 'deg)',
'-ms-transform': 'rotate(' + degree + 'deg)',
'transform': 'rotate(' + degree + 'deg)'
});
console.log(degree);
if (degree < 55) {
timer = setTimeout(function() {
rotate(++degree)
}, 10)
};
};

The code your supplied in the question should work in IE9.
The only reason I can think of for it not working is if your IE9 is actually rendering in IE8-compatibility mode.
This is actually quite a common problem (particularly for developers, as IE defaults to compatibility mode for sites on the local network, which generally includes your development server).
To check, open the dev tools menu (F12), and look at the top right; it should show you the browser mode. If it says anything other than "IE9 Standards", then you need to correct it. This annoying default can be switched off as follows:
Select the "Tools" menu,
Select Compatibility View Settings.
Un-tick the options that activate compatibility mode.
You might also want to add a meta tag to your page to force IE to use standards mode for other users:
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
With these measures in place, your page should render in IE9 standards mode, and you should then be able to use the standard CSS rotation.
Hope that helps.

try
if($.browser.msie){
$elie.css({ msTransform: 'rotate(' + degree + 'deg)' });
}else{
$elie.css({
'-webkit-transform': 'rotate(' + degree + 'deg)',
'-moz-transform': 'rotate(' + degree + 'deg)',
'-o-transform': 'rotate(' + degree + 'deg)',
'-ms-transform': 'rotate(' + degree + 'deg)'
});
}

Could you tell what IE browser version you are using ? your code should work fine for IE9,
Try to add this in your css for the other versions of IE :
filter: progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476); /* IE6,IE7 */
-ms-filter: "progid:DXImageTransform.Microsoft.Matrix(SizingMethod='auto expand', M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476)"; /* IE8 */
so with your code it should be like below:
function rotate(degree) {
$elie.css({
'-webkit-transform': 'rotate(' + degree + 'deg)',
'-moz-transform': 'rotate(' + degree + 'deg)',
'-o-transform': 'rotate(' + degree + 'deg)',
'-ms-transform': 'rotate(' + degree + 'deg)',
'transform': 'rotate(' + degree + 'deg)',
'filter': 'progid:DXImageTransform.Microsoft.Matrix(sizingMethod="auto expand", M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476)', /* IE7 Note this is the code for 45 degree */
'-ms-filter': "progid:DXImageTransform.Microsoft.Matrix(SizingMethod='auto expand', M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476)"; /* IE8 Note this is the code for 45 degree */
});
console.log(degree);
if (degree < 55) {
timer = setTimeout(function() {
rotate(++degree)
}, 10)
};
references :
CSS3 transform: rotate; in IE9
CSS rotate property in IE

Related

Set -webkit-transform and transform using JS

I am trying to change "transform" css property of element, using jQuery:
myBlock.css('-webkit-transform', 'rotate(' + angle + 'deg)');
myBlock.css('-moz-transform', 'rotate(' + angle + 'deg)');
myBlock.css('-ms-transform', 'rotate(' + angle + 'deg)');
myBlock.css('-o-transform', 'rotate(' + angle + 'deg)');
myBlock.css('transform', 'rotate(' + angle + 'deg)');
I want to see all of these properties applied to element, but the result is only
transform: rotate(45ged);
I tried JavaScript style, but it does not help too:
myBlock.style.WebkitTransform = "rotate(" + angle + "deg)";
Applied style is the same, as in previous example.
I found information, that modern jQuery versions (from 1.8) remove prefixes automatically. But why JS-style removes vendor properties?
To write all of these in one "attr('style', 'properties')" string is not a solution, because in this case it removes existing styles.
So the question is: how to apply all transform properties, using JS or jQuery?
Thanks for any help.
Edit: the goal is to have prefixed properties in saved html after executing JS command, not just to execute JS command and rotate div on angle's value.
Perhaps you could first retrieve the style attributes, append your own, and then set it again on the element.
var value = element.getAttribute('style');
value += 'your new style attributes';
element.setAttribute('style', value);
So, I have not found any good solution, except to modify "style" string. Here is it:
myBlock.css('transform', 'rotate(' + angle + 'deg)');
// Fix for old android versions:
var blockStyle = myBlock.attr('style');
if (blockStyle.indexOf('-webkit-transform') === -1) {
blockStyle += " -webkit-transform: rotate(" + angle + "deg);";
}
myBlock.attr('style', blockStyle);
The first line of code overwrites all transform properties, including prefixed. After its execution there will be no -webkit-transform part in style string. Then we add it.
In case of there still will be -webkit-transform in style string somehow after the first command (for me it never happen in modern Chrome, Firefox and Edge), I have else statement, a little bit ugly, but it works.
var blockStyle = myBlock.attr('style');
if (blockStyle.indexOf('-webkit-transform') === -1) {
blockStyle += " -webkit-transform: rotate(" + angle + "deg);";
} else {
var blockStyleArrayOriginal = blockStyle.split('-webkit-transform: rotate(');
blockStyle = blockStyleArrayOriginal[0].trim() + " -webkit-transform: rotate(" + angle + blockStyleArrayOriginal[1].substring(blockStyleArrayOriginal[1].indexOf('deg)'));
}
myBlock.attr('style', blockStyle);

Click to rotate goes in wrong direction at first

I am trying to have elements in a canvas rotate either clockwise or counter-clockwise depending on which arrow is clicked. The problem is that when you rotate one way and then another, the first click will go in the previously clicked arrow's direction on the first click.
ie. click rotate clockwise and it rotates clockwise. Then click rotate counter-clockwise and the first time you click it, the effected elements rotate clockwise on the then counter-clockwise on the second and each click afterwards. This happens in both directions.
Hope that's clear. Here, there's not much to it. Rotator code at the end:
$(document).ready(function(){
counter = 0;
//draggable
$(".drag").draggable({
helper:'clone',
containment: 'frame',
//first dragged
stop:function(ev, ui) {
var pos=$(ui.helper).offset();
objName = "#clonediv"+counter++
$(objName).css({"left":pos.left,"top":pos.top});
$(objName).removeClass("drag");
//existing dragged
$(objName).draggable({
containment: 'parent',
stop:function(ev, ui) {
var pos=$(ui.helper).offset();
console.log($(this).attr("id"));
console.log(pos.left)
console.log(pos.top)
}
});
}
});
//droppable
$("#frame").droppable({
drop: function(ev, ui) {
if (ui.helper.attr('id').search(/drag[0-9][0-9]/) != -1){
counter++;
var element=$(ui.draggable).clone();
element.addClass("tempclass");
$(this).append(element);
$(".tempclass").attr("id","clonediv"+counter);
$("#clonediv"+counter).removeClass("tempclass");
draggedNumber = ui.helper.attr('id').search(/drag([0-9][0-9])/)
itemDragged = "dragged" + RegExp.$1
console.log(itemDragged)
$("#clonediv"+counter).addClass(itemDragged);
$("ol").append($("div").attr("data-piece") + "<br>" );
}
}
});
//trash can
$("#trash").droppable({
greedy: 'true',
accept: function() { return true; },
drop: function (event, ui) {
tolerance: 'fit', $(ui.draggable).remove();
$("ol").detach();
}
});
//rotator
var angle = 22.5;
$('#spin').click(function() {
$('.drag').css ({
'-webkit-transform': 'rotate(' + angle + 'deg)',
'-moz-transform': 'rotate(' + angle + 'deg)',
'-o-transform': 'rotate(' + angle + 'deg)',
'-ms-transform': 'rotate(' + angle + 'deg)'
});
angle+=22.5;
});
$('#spin2').click(function() {
$('.drag').css ({
'-webkit-transform': 'rotate(' + angle + 'deg)',
'-moz-transform': 'rotate(' + angle + 'deg)',
'-o-transform': 'rotate(' + angle + 'deg)',
'-ms-transform': 'rotate(' + angle + 'deg)'
});
angle+=-22.5;
});
});
Any help is much appreciated!
Try moving the angle += ... lines before the $('.drag').css(...) lines.
Cleaned up a little, and starting at angle = 0:
// rotator
var angle = 0;
function rotateTo(angle) {
var value = 'rotate(' + angle + 'deg)';
$('.drag').css({
'-webkit-transform': value,
'-moz-transform': value,
'-o-transform': value,
'-ms-transform': value,
});
}
$('#spin').click(function() {
angle += 22.5;
rotateTo(angle);
});
$('#spin2').click(function() {
angle -= 22.5;
rotateTo(angle);
});

Continuous rotate image onclick of button

To display Image I used colorbox..In that I have add rotate-left and rotate-right to rotate the image..
The code is:
var rotate_right = document.getElementById('cboxRight');
$(rotate_right).on('click', function () {
var cboxphoto = document.getElementsByClassName('cboxPhoto')[0].style;
cboxphoto.setAttribute('-ms-transform', 'rotate(90deg)');
});
var rotate_left = document.getElementById('cboxLeft');
$(rotate_left).on('click', function () {
var cboxphoto = document.getElementsByClassName('cboxPhoto')[0].style;
cboxphoto.setAttribute('-ms-transform', 'rotate(270deg)');
});
It rotate 90deg if I click again on rightrotate button then it wont work..I want to rotate it again when click on button
You're only ever rotating to 90 or 270 degrees. When you click again, it doesn't move as it is already rotated to that angle.
Keep track of the current rotation instead and set the attribute to that value plus or minus 90deg - you can probably clean up the code a bit as well, but something like this fiddle: http://jsfiddle.net/w6ho689e/
var degrees = 0;
$("#cboxRight").on('click', function () {
var $cboxphoto = $('.cboxPhoto');
degrees += 90;
$cboxphoto.css('-ms-transform', 'rotate(' + degrees + 'deg)');
$cboxphoto.css('-webkit-transform', 'rotate(' + degrees + 'deg)');
$cboxphoto.css('transform', 'rotate(' + degrees + 'deg)');
});
$("#cboxLeft").on('click', function () {
var $cboxphoto = $('.cboxPhoto');
degrees -= 90;
$cboxphoto.css('-ms-transform', 'rotate(' + degrees + 'deg)');
$cboxphoto.css('-webkit-transform', 'rotate(' + degrees + 'deg)');
$cboxphoto.css('transform', 'rotate(' + degrees + 'deg)');
});

Center of the div - center point

How do I make the center of the div as the center point for the rotation.
I came across this while I was doing some research but I can't seem to fit it in to mine.
This is what the post has suggested. But doesn't work.
$(area).css('-webkit-transform-origin', 'rotate(' + dgR + 'deg)');
function rot(e, area) {
var offset = area.offset();
var ceX = (offset.left) + ($(area).width() /2);
var ceY = (offset.top) + ($(area).height() /2);
var muX = e.pageX;
var muY = e.pageY;
var rdi = Math.atan2(muX-ceX, muY-ceY);
var dgR = (rdi * (180/Math.PI)*-5);
$(area).css('transform', 'rotate(' + dgR + 'deg)');
$(area).css('-webkit-transform', 'rotate(' + dgR + 'deg)');
$(area).css('-o-transform', 'rotate(' + dgR + 'deg)');
$(area).css('-ms-transform', 'rotate(' + dgR+'deg)');
}
You're using the transform-origin incorrect.
$(area).css('-webkit-transform-origin', '50% 50%');
This should place the rotation origin point in the middle of the area
When I got you right, you just want to rotate a div in it's center point, aren't you? Here you have to define the center point with transform-origin: 50% 50%; in your div.
Heres a fiddle to play around (without jscript): http://jsfiddle.net/nub0umft/

jquery rotate function not working

Hi guys at the moment i am trying to figure out how to rotate a div and make it stop at a loctation. I have had some help on this however iam not sure what iam doing wrong. Any help with the code would be great.
Thanks guys for the help
try change your javascript code to be like this:
$(window).load(function(){
var $elie = $("#super");
rotate(1);
function rotate(degree) {
$elie.css({
'-webkit-transform': 'rotate(' + degree + 'deg)',
'-moz-transform': 'rotate(' + degree + 'deg)',
'-o-transform': 'rotate(' + degree + 'deg)',
'-ms-transform': 'rotate(' + degree + 'deg)',
'transform': 'rotate(' + degree + 'deg)'
});
console.log(degree);
if (degree < 100) {
timer = setTimeout(function() {
rotate(++degree)
}, 1);
}}
});
Here the demo: http://jsfiddle.net/ongisnade/mq8Ar/
Look at here http://jsbin.com/uwunaw/4/edit
I'm adding var elie = null; as global var and put elie = $("#super"); in onReady of jQuery.
Like this
$(function () {
elie = $("#super");
rotate(1);
});
It's working now.

Categories

Resources