js+css实现金属质感旋钮拖拽旋转数值变化效果代码

代码语言:html

所属分类:拖放

代码描述:js+css实现金属质感旋钮拖拽旋转数值变化效果代码

代码标签: js css 金属 质感 旋钮 拖拽 旋转 数值

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

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  

  
  
  
<style>
@import url("https://fonts.googleapis.com/css2?family=DM+Sans:wght@500&family=IBM+Plex+Serif&family=Poppins:wght@100;200&family=Raleway:wght@100;200&display=swap");
:root {
  --grainy: url("data:image/svg+xml,%3Csvg viewBox='0 0 250 250' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.829' numOctaves='6' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)'/%3E%3C/svg%3E");
}

:root {
  --opacityExp: "";
}

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
  font-family: "DM Sans";
  background: #eee;
  background: radial-gradient(closest-side, #fff, rgba(0, 0, 0, 0.4));
}
body::before {
  background: var(--grainy);
  mix-blend-mode: luminosity;
  content: "";
  display: block;
  inset: 0;
  opacity: 0.7;
  position: absolute;
  z-index: 0;
}

input[type=range] {
  --w: 200px;
  --w2: 12px;
  --angle: 160deg;
  --posx: calc(cos(var(--angle) + 135deg ) * (var(--w) / 2.4));
  --posy: calc(sin(var(--angle) + 135deg) * (var(--w) / 2.4));
  width: var(--w);
  height: var(--w);
  position: relative;
  cursor: grab;
}
input[type=range]::before {
  content: "";
  position: absolute;
  top: calc(50% - var(--w) / 2 - var(--w2) / 4);
  left: calc(50% - var(--w) / 2 - var(--w2) / 4);
  width: var(--w);
  height: var(--w);
  border-radius: 50%;
  background: rgba(173, 133, 133, 0.7);
  background: repeating-conic-gradient(#888 0%, #ddd 15%, #999 33%, #eee 40%, #aaa 50%);
  opacity: var(--opacityExp);
  border: 5px solid #aaa;
  box-shadow: 0 0 4px 15px #000;
  transform: rotate(var(--angle));
}
input[type=range]::after {
  content: "";
  display: block;
  position: absolute;
  top: calc(50% - var(--w2) / 2);
  left: calc(50% - var(--w2) / 2);
  translate: var(--posx) var(--posy);
  width: var(--w2);
  height: var(--w2);
  border-radius: 50%;
  border: 2px solid #444;
  background: radial-gradient(closest-side, #333, rgba(0, 0, 0, 0.5));
}

div {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5vw;
  text-align: center;
}

.knobVal {
  font-size: 10vw;
  color: #333;
  text-shadow: 3px 3px 2px #aaa;
  font-variant-numeric: tabular-nums;
  transition: color 0.4s ease-in-out;
  margin-top: 2rem;
  z-index: 2;
}
.knobVal.active {
  color: firebrick;
}

p {
  text-transform: uppercase;
  font-size: clamp(1.4rem, 3vw, 5vw);
  letter-spacing: 0.48em;
  z-index: 2;
}

.embed {
  height: 1px;
}

#link {
  margin-top: 8vh;
  opacity: 1;
  cursor: pointer;
  display: flex;
  gap: 0.5rem;
  height: 24px;
  z-index: 1;
  color: #777;
}
#link svg {
  fill: #1DA1F2;
}

.explanation {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  flex-direction: row;
  gap: 0.4rem;
}
</style>

  
</head>

<body>
  <div class="explanation">
  <label for="exp">How does this work ?</label>  
  <input type="checkbox" id="exp" />
</div>
<div>
  
  <div class="knobVal">60</div>
  <input id="rangeslider" type="range" value="60" min="0" max="100" step=".1">
  <p>Turn the knob</p>
  
</div>



      <script >
      // https://levelup.gitconnected.com/simplify-exporting-and-importing-of-your-javascript-utility-functions-2bfcb436c2d9
const utils = {
  norm: function (value, min, max) {
    return (value - min) / (max - min);
  },

  // lerp(a, b, x) = a + (b -a ) * x;
  // lerp(0, 100, 50) = 0 + (100 - 0) * 50
  // 0 - 127 > 0 - 100
  // = x / 127 * 100
  midintp: function (x, scale = 100) {
    return Math.floor((x / 127) * scale);
  },

  lerp: function (norm, min, max) {
    return (max - min) * norm + min;
  },

  map: function (value, sourceMin, sourceMax, destMin, destMax) {
    return utils.lerp(
      utils.norm(value, sourceMin, sourceMax),
      destMin,
      destMax
    );
  },

  clamp: function (value, min, max) {
    return Math.min(Math.max(value, Math.min(min, max)), Math.max(min, max));
  },

  distance: function (p0, p1) {
    var dx = p1.x - p0.x,
      dy = p1.y - p0.y;
    return Math.sqrt(dx * dx + dy * dy);
  },

  distanceXY: function (x0, y0, x1, y1) {
    var dx = x1 - x0,
      dy = y1 - y0;
    return Math.sqrt(dx * dx + dy * dy);
  },

  circleCollision: function (c0, c1) {
    return utils.distance(c0, c1) <= c0.radius + c1.radius;
  },

  circlePointCollision: function (x, y, circle) {
    return utils.distanceXY(x, y, circle.x, circle.y) < circle.radius;
  },

  pointInRect: function (x, y, rect) {
    return (
      utils.inRange(x, rect.x, rect.x + rect.width) &&
      utils.inRange(y, rect.y, rect.y + rect.height)
    );
  },

  inRange: function (value, min, max) {
    return value >= Math.min(min, max) && value <= Math.max(min, max);
  },

  rangeIntersect: function (min0, max0, min1, max1) {
    return (
      Math.max(min0, max0) >= Math.min(min1, max1) &&
      Math.min(min0, max0) <= Math.max(min1, max1)
    );
  },

  rectIntersect: function (r0, r1) {
    return (
      utils.rangeIntersect(r0.x, r0.x + r0.width, r1.x, r1.x + r1.width) &&
      utils.rangeIntersect(r0.y, r0.y + r0.height, r1.y, r1.y + r1.height)
    );
  },

  degreesToRads: function (degrees) {
    return (degrees / 180) * Math.PI;
  },

  radsToDegrees: function (radians) {
    return (radians * 180) / Math.PI;
  },

  roundToPlaces: function (value, places) {
    var mult = Math.pow(10, places);
    return Math.round(value * mult) / mult;
  },

  roundNearest: function (value, nearest) {
    return Math.round(value / nearest) * nearest;
  },

  isInArray: function (value, array) {
    return array.indexOf(value) > -1;
  },

  setCustomProp: function (prop, val, unit = "") {
    document.documentElement.style.setProperty("--" + prop, val + unit);
  },

  getRandom: function (min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.random() * (max - min) + min; //The maximum is exclusive and the minimum is inclusive
  },
  getRandomInt: function (min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min) +.........完整代码请登录后点击上方下载按钮下载查看

网友评论0