js实现页面图片放大预览及旋转

1.图片预览model

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="modal">
   <div>
      <div id="myModal" class="modal">
         <span class="close cursor" onclick="closeModal()">&times;</span>
         <div class="modal-content">
            <div class="mySlides">
               <img id="bigImg" :src="previewImg" style="width:100%;height:500px;position: absolute;">
               <span class="rotate glyphicon glyphicon-repeat" @click="rotate()"></span>
            </div>
         </div>
      </div>
   </div>
</div>
</body>
</html>
2.图片css样式
body {
  font-family: Verdana, sans-serif;
  margin: 0;
}

* {
  box-sizing: border-box;
}

.row > .column {
  padding: 0 8px;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

.column {
  float: left;
  width: 25%;
}

/* The Modal (background) */
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  /*background-color: black;*/
  background-color: rgba(48, 49, 51, 0.64);
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  width: 55%;
  height:500px;
  max-width: 1200px;
}

/* The rotate Button */
.rotate {
  color: black;
  position: absolute;
  top: 467px;
  right: 0px;
  font-size: 35px;
  font-weight: bold;
}

.close {
  color: white;
  position: absolute;
  top: 10px;
  right: 25px;
  font-size: 35px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #999;
  text-decoration: none;
  cursor: pointer;
}

.mySlides {
  display: none;
}

.cursor {
  cursor: pointer
}

/* Next & previous buttons */
.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  padding: 16px;
  margin-top: -50px;
  color: white;
  font-weight: bold;
  font-size: 20px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
  -webkit-user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

img {
  margin-bottom: -4px;
}

.caption-container {
  text-align: center;
  background-color: black;
  padding: 2px 16px;
  color: white;
}

.demo {
  opacity: 0.6;
}

.active,
.demo:hover {
  opacity: 1;
}

img.hover-shadow {
  transition: 0.3s
}

.hover-shadow:hover {
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
3.图片预览js方法
function openModal() {
    document.getElementById('myModal').style.display = "block";
}

function closeModal() {
    document.getElementById('myModal').style.display = "none";
}

var slideIndex = 1;
showSlides(slideIndex);

function currentSlide(n) {
    showSlides(slideIndex = n);
}

function showSlides(n) {
    var i;
    var slides = document.getElementsByClassName("mySlides");
    if (n > slides.length) {slideIndex = 1}
    if (n < 1) {slideIndex = slides.length}
    for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";
    }
    slides[slideIndex-1].style.display = "block";
}
var rotateNum=0;
//图片旋转
function rotate(){
    $("#bigImg").css("height","");
    $("#bigImg").css("width","");
    if(rotateNum==0){
        $("#bigImg").css("width","500px");
        $("#bigImg").css("height","100%");
        $("#bigImg").css("transform","rotate(90deg)");
        rotateNum=90;
    }else if(rotateNum==90){
        $("#bigImg").css("width","100%");
        $("#bigImg").css("height","500px");
        $("#bigImg").css("transform","rotate(180deg)");
        rotateNum=180;
    }else if(rotateNum==180){
        $("#bigImg").css("transform","rotate(270deg)");
        $("#bigImg").css("width","500px");
        $("#bigImg").css("height","100%");
        rotateNum=270;
    }else if(rotateNum==270){
        $("#bigImg").css("transform","rotate(0deg)");
        $("#bigImg").css("width","100%");
        $("#bigImg").css("height","500px");
        rotateNum=0;
    }
}
4.html页面调用事件
<div class="col-sm-6" style="position: relative;top: 25px">
   <div class="row">
      <div class=" col-sm-12">
         <img  :src="previewImg" alt="" style="position: relative;width:100%;height: 300px;" onclick="openModal()" class="hover-shadow cursor">
      </div>
   </div>
   <div th:include="lightbox/myModal::modal"></div>
</div>

猜你喜欢

转载自blog.csdn.net/zwl18210851801/article/details/80061958