JS实现图片左右旋转90度

话不多说,直接上代码,可以直接复制到html文件运行看效果
代码如下:

<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>
  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值