Rotate image with javascript

后端 未结 9 636
温柔的废话
温柔的废话 2020-11-27 04:17

I need to rotate an image with javascript in 90-degree intervals. I have tried a few libraries like jQuery rotate and Raphaël, but they have the same problem - The image is

相关标签:
9条回答
  • 2020-11-27 04:28
    var angle = 0;
    $('#button').on('click', function() {
        angle += 90;
        $('#image').css('transform','rotate(' + angle + 'deg)');
    });
    

    Try this code.

    0 讨论(0)
  • 2020-11-27 04:28

    i have seen your running code .There is one line correction in your code.

    Write:

    $("#wrapper").rotate(angle); 
    

    instead of:

    $("#image").rotate(angle);
    

    and you will get your desired output,hope this is what you want.

    0 讨论(0)
  • 2020-11-27 04:32

    I think this will work.

        document.getElementById('#image').style.transform = "rotate(90deg)";
    

    Hope this helps. It's work with me.

    0 讨论(0)
  • 2020-11-27 04:36

    You use a combination of CSS's transform (with vendor prefixes as necessary) and transform-origin, like this: (also on jsFiddle)

    var angle = 0,
      img = document.getElementById('container');
    document.getElementById('button').onclick = function() {
      angle = (angle + 90) % 360;
      img.className = "rotate" + angle;
    }
    #container {
      width: 820px;
      height: 100px;
      overflow: hidden;
    }
    #container.rotate90,
    #container.rotate270 {
      width: 100px;
      height: 820px
    }
    #image {
      transform-origin: top left;
      /* IE 10+, Firefox, etc. */
      -webkit-transform-origin: top left;
      /* Chrome */
      -ms-transform-origin: top left;
      /* IE 9 */
    }
    #container.rotate90 #image {
      transform: rotate(90deg) translateY(-100%);
      -webkit-transform: rotate(90deg) translateY(-100%);
      -ms-transform: rotate(90deg) translateY(-100%);
    }
    #container.rotate180 #image {
      transform: rotate(180deg) translate(-100%, -100%);
      -webkit-transform: rotate(180deg) translate(-100%, -100%);
      -ms-transform: rotate(180deg) translateX(-100%, -100%);
    }
    #container.rotate270 #image {
      transform: rotate(270deg) translateX(-100%);
      -webkit-transform: rotate(270deg) translateX(-100%);
      -ms-transform: rotate(270deg) translateX(-100%);
    }
    <button id="button">Click me!</button>
    <div id="container">
      <img src="http://i.stack.imgur.com/zbLrE.png" id="image" />
    </div>

    0 讨论(0)
  • 2020-11-27 04:36

    You can always apply CCS class with rotate property - http://css-tricks.com/snippets/css/text-rotation/

    To keep rotated image within your div dimensions you need to adjust CSS as well, there is no needs to use JavaScript except of adding class.

    0 讨论(0)
  • 2020-11-27 04:40

    No need for jQuery and lot's of CSS anymore (Note that some browsers need extra CSS)

    Kind of what @Abinthaha posted, but pure JS, without the need of jQuery.

    let rotateAngle = 90;
    
    function rotate(image) {
      image.setAttribute("style", "transform: rotate(" + rotateAngle + "deg)");
      rotateAngle = rotateAngle + 90;
    }
    #rotater {
      transition: all 0.3s ease;
      border: 0.0625em solid black;
      border-radius: 3.75em;
    }
    <img id="rotater" onclick="rotate(this)" src="https://upload.wikimedia.org/wikipedia/en/e/e0/Iron_Man_bleeding_edge.jpg"/>

    0 讨论(0)
提交回复
热议问题