如何实现图片垂直旋转90度的问题

非常简单的问题,一串代码就可以解决。复制修改一下就可以直接使用,一个简单的小demo。写项目的时候需要写的功能,不到二十行代码就可以实现。

复制代码
<html>
<head>
    <title>旋转图片</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html
        {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            background-color: transparent;
        }
        /*包裹图片div样式*/
        #allDiv
        {
            font-family: "微软雅黑";
            font: 14px/1.8 arial;
        }
        /*图片样式*/
        .demoImg
        {
            background-color: Gray;
            border: none;
            margin-bottom: 50px;
        }
        .bottom
        {
            width: 100%;
            position: fixed;
            bottom: 10px;
            text-align: center;
        }
        .btn
        {
            cursor: pointer;
            width: 100px;
            height: 35px;
            background: rgba(38, 38, 38, 0.6);
            border: 0px;
            color: white;
        }
    </style>
</head>
<body>
    <div id="allDiv" style="text-align: center;">
        <img id="demoImg" alt="图片" class="demoImg" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600861823902&di=29dac776c7336304cc69dc01983cc779&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201607%2F18%2F20160718205958_EzVsa.jpeg" />
    </div>
    <div class="bottom">
        <div class="col-md-12 col-sm-12 col-xs-12 text-center">
            <button class="btn" type="button" onclick="SetImgRotate(0)" id="btnLeft">
                左旋转</button>
            <button class="btn" type="button" onclick="SetImgRotate(1)" id="btnRight">
                右旋转</button>
        </div>
    </div>
    <script type="text/javascript">
        //旋转图片
        var current = 0;
        function SetImgRotate(leftOrRight) {
            var img = document.getElementById('demoImg');
            if (leftOrRight == 0) {//左旋转
                current = (current - 90) % 360;
            }
            else if (leftOrRight == 1) {//右旋转
                current = (current + 90) % 360;
            }
            img.style.transform = 'rotate(' + current + 'deg)';
        }
    </script>
</body>
</html>
相关推荐
Cocktail_py23 分钟前
JS如何调用wasm
开发语言·javascript·wasm
Jonathan Star2 小时前
基于 **Three.js** 开发的 3D 炮弹发射特效系统
javascript·数码相机·3d
Heo2 小时前
原型理解从入门到精通
前端·javascript·后端
Heo2 小时前
通用会话控制方案
前端·javascript·后端
Heo2 小时前
跨域问题解决方案汇总
前端·javascript·后端
shmily麻瓜小菜鸡2 小时前
Element Plus 的 <el-table> 怎么点击请求后端接口 tableData 进行排序而不是网络断开之后还可以自己排序
前端·javascript·vue.js
二川bro2 小时前
第38节:WebGL 2.0与Three.js新特性
开发语言·javascript·webgl
xiaoxue..2 小时前
深入理解 JavaScript 异步编程:从单线程到 Promise 的完整指南
前端·javascript·面试·node.js
倚肆3 小时前
HTMLElement 与MouseEvent 事件对象属性详解
前端·javascript
青衫码上行3 小时前
【Java Web学习 | 第12篇】JavaScript(6)DOM
java·开发语言·前端·javascript·学习