如何实现图片垂直旋转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>
相关推荐
安卓开发者4 分钟前
第4讲:理解Flutter的灵魂 - “Everything is a Widget”
开发语言·javascript·flutter
im_AMBER39 分钟前
React 09
前端·javascript·笔记·学习·react.js·前端框架
ejinxian1 小时前
Rust UI 框架GPUI 与 Electron 的对比
前端·javascript·electron
小马哥learn1 小时前
Vue3 + Electron + Node.js 桌面项目完整开发指南
前端·javascript·electron
涔溪2 小时前
在 Electron 框架中连接 OPC UA 服务器并读取 PLC 数据
服务器·javascript·electron
前端小咸鱼一条2 小时前
19. React的高阶组件
前端·javascript·react.js
狮子座的男孩2 小时前
js基础:10、函数对象方法(call/apply)、arguments类数组对象、Date对象、Math工具类、包装类、字符串方法、正则表达式
前端·javascript·正则表达式·包装类·字符串方法·arguments·date对象
qq_338032923 小时前
VUE的生命周期钩子,vue2和vue3的生命周期钩子的核心差异
前端·javascript·vue.js
天天向上10243 小时前
在 Vue3 项目中使用 el-tree
javascript·vue.js·elementui
天天向上10243 小时前
vue2 vue3 修改elementUI和elementPlus主题颜色
前端·javascript·elementui