Set perspective-origin with jQuery .css()

1.4k views Asked by At

I want to set dynamically with jQuery the css value of perspective-origin of a div. It's an absolute div(popup) with dynamic top and left (depends of window size, scroll etc...).

When I open the popup I want to do an animation in css with transform: translateZ(-150px); But I have to set a perspective-origin as the center of the popup.

In javacript i did the following code

center: function () {

        var $popup = this.$( '.popup' );
        var $popupOverlay = this.$( '.popup-overlay' );
        var wWidth = $( window ).width();
        var wHeight = $( window ).height();
        var dHeight = $( document ).height();
        var popupWidth = $popup.width();
        var popupHeight = $popup.height();

        // Popup centered in the window at the scroll position
        var popupTop = ( wHeight / 2 ) - ( popupHeight / 2 ) + $( document ).scrollTop();
        var popupLeft = ( wWidth / 2 ) - ( popupWidth / 2 );

        // If the popup height bigger than window height then the popup is not centered in the window but sticked at top
        if ( popupHeight > wHeight ) {
            popupTop += ( popupHeight - wHeight ) / 2;
        }

        // Set popupOverlay with and height as the document
        $popupOverlay.width( wWidth );
        $popupOverlay.height( dHeight );

        // Set calculated top and left offset to the popup
        $popup.css( {
            'left': popupLeft,
            'top': popupTop
        } );

        // Now calculate the transform-origin center of the popup
        var xPos = popupLeft + popupWidth / 2 + 'px';
        var yPos = popupTop + popupHeight / 2 + 'px';
        var xyPos = xPos + ' ' + yPos;
        console.log(xyPos);

        // Set calculated transform-origin to the parent

        this.$( '.popup-container' ).css( {
            'perspective-origin': xyPos
        } );
    }

console.log xyPos return 538.5px 3024.5px so that should work...

When I inspect .popup-container I see no perspective-origin set.

For info I allready try to set -webkit-perspective-origin but didn't work too.

Does Jquery .css() methode handle perspective-origin?

Thanks in advance for your answers.

1

There are 1 answers

0
amazedinc On BEST ANSWER

jQuery .css() method does handle perspective origin. For a quick reference that this feature works please check the source and you'll see perspective origin being set via style=""

http://jsfiddle.net/remix1201/d3hnpsgz/

HTML:

<div class="popup">This is a div class named popup.</div>

JS: (jQuery)

var xypos = "10px 20px"
$(".popup").css("perspective-origin", xypos);
$(".popup").css("color", "blue");

If you would like to make a fiddle to implement your function then we can take a better look at just exactly what is going wrong with your script.

Reference Image Showing Perspective Origin Being Set