原生js实现图片放大镜效果

代码语言:html

所属分类:图片放大

代码描述:原生js实现图片放大镜效果

代码标签: 图片 放大镜 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<style>
*,
*:after,
*:before {
  box-sizing: border-box;
}
:root {
  --viewfinder-scale: 4;
  --viewfinder-size-scale: 0.5;
  --size: 50;
}
body {
  -webkit-box-align: center;
          align-items: center;
  display: -webkit-box;
  display: flex;
  min-height: 100vh;
  -webkit-box-pack: center;
          justify-content: center;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
          flex-direction: column;
  background: #8ce1f2;
}
h1 {
  position: fixed;
  right: 2rem;
  bottom: 2rem;
  margin: 0;
  font-family: sans-serif;
  color: #149cb8;
}
.zoomer {
  box-shadow: 1vmin 1vmin 2vmin 1vmin #000;
  height: calc(var(--size, 50) * 1vmin);
  width: calc(var(--size, 50) * 1vmin);
  position: relative;
}
.zoomer__img {
  --color-one: #f2f2f2;
  --color-two: #d9d9d9;
  height: 100%;
  width: 100%;
  background: -webkit-gradient(linear, left top, left bottom, from(var(--color-one)), color-stop(var(--color-two)), to(var(--color-one)));
  background: linear-gradient(var(--color-one), var(--color-two), var(--color-one));
}
.zoomer__img:not([src]) {
  -webkit-animation: roll 1s infinite linear;
          animation: roll 1s infinite linear;
}
.zoomer__img:not([src]) ~ .zoomer__viewfinder {
  display: none;
}
.zoomer__viewfinder {
  --show: 0;
  cursor: none;
  height: calc((var(--size) * var(--viewfinder-size-scale)) * 1vmin);
  width: calc((var(--size) * var(--viewfinder-size-scale)) * 1vmin);
  position: absolute;
  top: calc(var(--y, 50) * 1%);
  left: calc(var(--x, 50) * 1%);
  -webkit-transition: -webkit-transform 0.25s ease;
  transition: -webkit-transform 0.25s ease;
  transition: transform 0.25s ease;
  transition: transform 0.25s ease, -webkit-transform 0.25s ease;
  -webkit-transform: translate(-50%, -50%) scale(var(--show));
          transform: translate(-50%, -50%) scale(var(--show));
}
.zoomer__viewfinder-image-wrapper {
  border-radius: 50%;
  height: 100%;
  width: 100%;
  overflow: hidden;
  position: relative;
}
.zoomer__magnifying-glass {
  position: absolute;
  top: 0;
  left: 0;
  -webkit-transform: scale(1.2) translate(4%, 5%);
          transform: scale(1.2) translate(4%, 5%);
  z-index: 2;
}
.zoomer__viewfinder-img {
  height: 100%;
  width: 100%;
  position: absolute;
  -webkit-transform: scale(var(--viewfinder-scale)) translate(calc(var(--x, 0) * 1%), calc(var(--y, 0) * 1%));
          transform: scale(var(--viewfinder-scale)) translate(calc(var(--x, 0) * 1%), calc(var(--y, 0) * 1%));
  -webkit-transform-origin: center center;
          transform-origin: center center;
}
.zoomer__viewfinder-img:not([src]) {
  display: none;
}
.magnifying-glass__glass {
  fill: rgba(204,246,255,0.3);
}
.magnifying-glass__frame {
  fill: #999;
  stroke: #000;
}
.magnifying-glass__handle {
  stroke: #000;
  fill: #737373;
}
.magnifying-glass__grip {
  fill: #5c250a;
  stroke: #000;
}
.magnifying-glass__shine {
  stroke: rgba(255,255,255,0.65);
  fill: none;
}
@-webkit-keyframes roll {
  to {
    background-position: 0 calc(var(--size, 50) * 1vmin);
  }
}
@keyframes roll {
  to {
    background-position: 0 calc(var(--size, 50) * 1vmin);
  }
}
</style>

</head>
<body translate="no">
<div class="zoomer"><img class="zoomer__img" />
<div class="zoomer__viewfinder">
<svg class="magnifying-glass zoomer__magnifying-glass" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 245 328">
<g stroke-linecap="round" stroke-linejoin="round" transform="rotate(-30 57 -316)">
<rect class="magnifying-glass__handle" width="11.3" height="98.1" x="-115" y="186" ry=".6" stroke-width="5.3"></rect>
<rect class="magnifying-glass__grip" width="27.8" height="113" x="-123" y="211" ry="9.1" stroke-width="5.3"></rect>
<g class="magnifyi.........完整代码请登录后点击上方下载按钮下载查看

网友评论0