JS 实现兼容IE图片向左或向右翻转

Posted minoz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS 实现兼容IE图片向左或向右翻转相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
    <head>
        <title>JS实现图片向左向右翻转</title>
        <meta http-equiv="content-type" content="text/html;charset=UTF-8">
        <script type="text/javascript">
            function rotate(o, p) {
                var img = document.geiElmentById(o);
                if(!img || !p) return false;
                var n = img.getAttribute(step);
                if(n === null)    n = 0;
                if(p === right) {
                    (n === 3) ? n = 0 : n++;
                } else if(p === left) {
                    (n === 0) ? n = 3 : n--;
                }

                img.setAttribute(step, n);
                // MSIE
                if(document.all) {
                    img.style.filter = progid:DXImageTransform.Microsoft.BasicImage(rotation= + n +);
                    switch(n) {
                        case 0:
                            img.parentNode.style.height = img.height;
                            break;
                        case 1:
                            img.parentNode.style.height = img.width;
                            break;
                        case 2:
                            img.parentNode.style.height = img.height;
                            break;
                        case 3:
                            img.parentNode.style.height = img.width;
                            break;
                    }
                } else {
                    var c = document.getElementById(canvas + o);
                    if(c === null) {
                        img.style.visibility = hidden;
                        img.style.position = absolute;
                        c = document.createElement(canvas);
                        c.setAttribute("id", canvas + o);
                        img.parentNode.appendChild(c);
                    }
                
                   var canvasContent = c.getContext(2d);
                    switch(n) {
                        default: 
                        case 0:
                            c.setAttribute(width, img.width);
                            c.setAttribute(height, img.height);
                            canvasContent.rotate(0 * Math.PI / 180);
                            canvasContent.drawImage(img, 0, 0);
                            break;
                        case 1:
                            c.setAttribute(width, img.width);
                            c.setAttribute(height, img.height);
                            canvasContent.rotate(90 * Math.PI / 180);
                            canvasContent.drawImage(img, 0, -img.height);
                            break;
                        case 2:
                            c.setAttribute(width, img.width);
                            c.setAttribute(height, img.height);
                            canvasContent.rotate(180 * Math.PI / 180);
                            canvasContent.drawImage(img, -img.width, -img.height);
                            break;
                        case 3:
                            c.setAttribute(width, img.width);
                            c.setAttribute(height, img.height);
                            canvasContent.rotate(270 * Math.PI / 180);
                            canvasContent.drawImage(img, -img.width, 0);
                            break;
                    }
                }
            }
        </script>
    </head>
    <body>
        <div class="container">
            <input type="button" value="turn left" onclick="rotate(‘pic‘, ‘left‘)" />
            <input type="button" value="turn right" onclick="rotate(‘pic‘, ‘right‘)" />
            <div class="cont" onclick="rotate(‘pic‘, ‘right‘)">
                <img id="pic" src="1.jpg" alt="" />
            </div>
        </div>
    </body>
</html>

 

以上是关于JS 实现兼容IE图片向左或向右翻转的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中检测向左或向右滑动?

UITableViewCell 如何检测用户向左或向右滑动?

在 UITableViewCell 中的任意位置向左或向右滑动以删除没有删除按钮的单元格?

向左或向右移动 UINavigationBar 标题的文本

iPhone:如何向左或向右旋转 UIImageview?

在 PyQt5 中将 QsciScintilla 的文本光标向左或向右移动