jQuery元素自由旋转。在IE中修正变换原点和平移

4
我正在开发一个jQuery插件,可以通过鼠标使块级元素旋转。现在在非IE浏览器中按预期工作,但在Internet Explorer中旋转时有奇怪的行为。
演示网址testerski.antaranian.me,旋转插件脚本如下:
    $.fn.roll = function(angle){
    var $this = this,
        ie = !jQuery.support.leadingWhitespace;
    if (ie) {
        var cosAngle = parseFloat(parseFloat(Math.cos(angle.rad())).toFixed(8)),
            sinAngle = parseFloat(parseFloat(Math.sin(angle.rad())).toFixed(8)),
            tx = 0, ty = 0,
            matrixFilter = '(M11=' + cosAngle + ', ' 
                    + 'M12=' + -sinAngle + ', '
                    + 'M21=' + sinAngle + ', '
                    + 'M22=' + cosAngle + ','
                    + 'sizingMethod=\'auto expand\')',
            filter = 'progid:DXImageTransform.Microsoft.Matrix' + matrixFilter,
            css = {
                '-ms-filter': filter,
                'filter': filter                        
              };
            debug.log(filter);  
        var matrix = $M([
                  [cosAngle, -sinAngle, tx],
                  [sinAngle, cosAngle, ty],
                  [0, 0, 1]
                ]);  
        debug.log(matrix);
        $this.transformOrigin(matrix);
        $this.fixIeBoundaryBug(matrix);

    } else {
        var css = {
                '-webkit-transform': 'rotate(' + angle + 'deg)',
                '-moz-transform': 'rotate(' + angle + 'deg)',
                '-o-transform': 'rotate(' + angle + 'deg)'
              };
    }   
    $this.css(css);
    return this;            
  };

我谷歌搜索了与这个主题相关的两个页面:

Grady's guide Zoltan's guide

我发现线性代数需要一些会计学知识,但对我来说很难,如果有人有更简单的教程或知道直接的解决方案,请告诉我。任何帮助都将不胜感激,Antaranian。
3个回答

3

不幸的是,IE的Transform Filter没有“transform-origin”的概念。'auto expand' sizingMethod会使变换后的对象占用最少的空间,您需要更改它的定位。

在cssSandpaper中,我在变换后的对象周围放置了另一个<div>标签,并调整了其margin-left和margin-top。如果您访问cssSandpaper网站并查看代码,则会看到确切的公式(在cssSandpaper.js中搜索“setMatrixFilter”)。您可以将其硬编码到库中,也可以使用cssSandpaper本身来执行此操作(使用cssSandpaper.setTransform()方法)。即使它可能会向您的代码添加几KB,我建议这样做,以防未来我改进了处理变换的方式。

无论如何,祝你好运!

Z.


0

实际上,我已经根据自己的需求编写了代码,如果其他人有兴趣,这里是代码。

$.fn.ieRotate = function(alfa){
    var self = this,
        cosAlfa = Math.cos(alfa),
        sinAlfa = Math.sin(alfa),
        matrix = '(M11=' + cosAlfa + ', ' 
                + 'M12=' + -sinAlfa + ', '
                + 'M21=' + sinAlfa + ', '
                + 'M22=' + cosAlfa + ','
                + 'sizingMethod=\'auto expand\')',
        // constructing the final filter string
        filter = 'progid:DXImageTransform.Microsoft.Matrix' + matrix;    
    self.each(function(el){
        var $this = $(el),
            size = $this.data('size'),
            pos = $this.data('pos');
        $this.css({
            '-ms-filter': filter,
            'filter': filter,
            // for IE9
            'transform': 'rotate(' + angle + 'deg)'
          });
        // calculate the difference between element's expeced and the actual centers
        var dLeft = ($this.width() - size.width) / 2,
            dTop = ($this.height() - size.height) / 2;
        $this.css({
            top: pos.top -dTop,
            left: pos.left - dLeft 
          });   
    });
    return self;    
};

使用方法:

// caching the image object to a variable
$image = $('img#my-image');
// saving images non-rotated position and size data
$image.data('pos', {
        top:    $image.position().top,
        left:   $image.position().left
    }).data('size', {
        height: $image.height(),
        width:  $image.width()
    });
// rotate image 1.2 radians
$image.ieRotate(1.2);

感谢 @Zoltan Hawryluk,他的代码在开发过程中帮了我很大的忙。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接