Zoom in and out on mouse click with CSS

前端 未结 5 561
我寻月下人不归
我寻月下人不归 2021-02-05 11:49

I want to zoom image with only CSS. The code below zooms the image when the left button of the mouse is kept pressed but I want to zoom in and out with a mouse click. How can I

5条回答
  •  别那么骄傲
    2021-02-05 12:10

    Building on @Nhan answer: https://stackoverflow.com/a/39859268/661872

    Shorter, scoped and does not require tracking ids for multiple elements.

    .click-zoom input[type=checkbox] {
      display: none
    }
    
    .click-zoom img {
      margin: 100px;
      transition: transform 0.25s ease;
      cursor: zoom-in
    }
    
    .click-zoom input[type=checkbox]:checked~img {
      transform: scale(2);
      cursor: zoom-out
    }

提交回复
热议问题