Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

javascript - Cross-Browser-Problem: transform: rotateX() – JS + CSS

I wrote this script here: https://jsfiddle.net/g130hqv4/2/

This is what's supposed to happen: I move the cursor across the page and the div in the middle of the page slightly follows the position of the cursor and rotates into that direction. (Also see screenshots.)

Screenshot-1: Cursor is in the middle of the page: Cursor is in the middle of the page Screenshot-2: Cursor is in the upper left corner: Cursor is in the upper left corner

While I was working on this, I used Safari and now I realised that it only works in Safari, I also tested Opera and Chrome and the div moves around as expected but it does not rotate. I though the problem had to be: CSS3 3D Transforms, or: perspective: 500;, but I checked CanIUse.com and it's all supported, what's going on?

–––––––––––

This is my JS:

var overlayDiv = document.querySelector(".hello");

var moveDivMaxX = undefined;
var moveDivMaxY = undefined;

var viewport_center = {
    x: undefined,
    y: undefined
};

var mouse = {
    x: undefined,
    y: undefined
};

var differenceX = undefined;
var differenceY = undefined;

var moveDivX = undefined;
var moveDivY = undefined;

// tilt START
var tiltX = undefined;
var titlY = undefined;
    
var MaxTiltX = 10;
var MaxTitlY = 10;
//tilt END

function load() {
    moveDivMaxX = window.innerWidth * 0.08;
    moveDivMaxY = window.innerHeight * 0.08;
    
    viewport_center.x = window.innerWidth / 2;
    viewport_center.y = window.innerHeight / 2;
}

window.addEventListener('mousemove', function(e) {
    mouse.x = e.x;
    mouse.y = e.y;
    
    differenceX = viewport_center.x - mouse.x;
    differenceX = differenceX * -1;
    
    differenceY = viewport_center.y - mouse.y;
    differenceY = differenceY * -1;
    
    var moveDivXinPercent = differenceX * 100 / viewport_center.x;
    moveDivX = moveDivMaxX / 100 * moveDivXinPercent;
    
    var moveDivYinPercent = differenceY * 100 / viewport_center.y;
    moveDivY = moveDivMaxY / 100 * moveDivYinPercent;
    
    
    var centerDivX = viewport_center.x - (document.querySelector(".hello").clientWidth / 2);
    var centerDivY = viewport_center.y - (document.querySelector(".hello").clientHeight / 2);
    
    overlayDiv.style.left = centerDivX + moveDivX + "px";
    overlayDiv.style.top = centerDivY + moveDivY + "px";
    
    // Tilt hello START
    tiltX = MaxTiltX / 100 * moveDivYinPercent * -1;
    titlY = MaxTitlY / 100 * moveDivXinPercent;
  
    overlayDiv.style.transform = "rotateX(" + tiltX + "deg) rotateY(" + titlY + "deg)";
    // Tilt hello END
})

window.onresize = load;
load();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Heureka! I found the problem:

I changed this: perspective: 500; to this: perspective: 500px;

I, again, tested this in Safari, Opera and Chrome and it works. :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...