图像比较滑块带有对角手柄

huangapple go评论77阅读模式
英文:

Image comparison slider with diagonal handle

问题

我需要制作一个图像比较滑块,通常的滑块上有两个图像和一个手柄,允许你显示/隐藏这些图像。就像这个例子一样:https://codyhouse.co/demo/image-comparison-slider/index.html

不同之处在于手柄必须是垂直的,但不是完全垂直的,它必须有一个角度,就像下面的图片一样。然后它是水平拖动的。你会如何实现这个?
图像比较滑块带有对角手柄

英文:

I need to make an image comparison slider, the usual slider where you have two images and a handle that allows you to show/hide the images.
Like this one for example https://codyhouse.co/demo/image-comparison-slider/index.html

The twist is that the handle must be vertical, but not perfectly vertical, it must have an angle, like in the image below. Then it's dragged horizontally. How would you achieve this?
图像比较滑块带有对角手柄

答案1

得分: 1

考虑以下内容。

JavaScript

$(function() {
  function makeClip(width, height, mid) {
    //left, top
    var half = 50;
    var coords = [
      [0, 0],
      [mid + half, 0],
      [mid - half, height],
      [0, height]
    ];
    return coords;
  }

  function setClipStyle(target, left) {
    var coords = makeClip($(target).width(), $(target).height(), left);
    console.log(coords);
    var parts = [];
    parts.push(coords[0][0] + "px " + coords[0][1] + "px");
    parts.push(coords[1][0] + "px " + coords[1][1] + "px");
    parts.push(coords[2][0] + "px " + coords[2][1] + "px");
    parts.push(coords[3][0] + "px " + coords[3][1] + "px");

    var style = 'polygon(' + parts.join(",") + ')';
    console.log(style);
    $(target).data("mid", left).css("clipPath", style);
  }

  setClipStyle(".img-over img", 300);
  $(".slide-handle").draggable({
    axis: "x",
    containment: [-20, 0, 580, 0],
    drag: function(e, ui) {
      setClipStyle(".img-over img", ui.position.left + 30);
    }
  });
});

这会操纵具有 clip-path 的图像的CSS。您需要检查浏览器兼容性。我还使用了jQuery UI来更容易地处理拖动操作。

参考资料:

英文:

Consider the following.

https://jsfiddle.net/Twisty/jzeo4xsm/82/

JavaScript

$(function() {
  function makeClip(width, height, mid) {
    //left, top
    var half = 50;
    var coords = [
      [0, 0],
      [mid + half, 0],
      [mid - half, height],
      [0, height]
    ];
    return coords;
  }

  function setClipStyle(target, left) {
    var coords = makeClip($(target).width(), $(target).height(), left);
    console.log(coords);
    var parts = [];
    parts.push(coords[0][0] + "px " + coords[0][1] + "px");
    parts.push(coords[1][0] + "px " + coords[1][1] + "px");
    parts.push(coords[2][0] + "px " + coords[2][1] + "px");
    parts.push(coords[3][0] + "px " + coords[3][1] + "px");

    var style = 'polygon(' + parts.join(",") + ')';
    console.log(style);
    $(target).data("mid", left).css("clipPath", style);
  }

  setClipStyle(".img-over img", 300);
  $(".slide-handle").draggable({
    axis: "x",
    containment: [-20,0,580,0],
    drag: function(e, ui) {
      setClipStyle(".img-over img", ui.position.left + 30);
    }
  });
});

This manipulates the CSS of the Image with clip-path. You will need to check on Browser Compatibility. I also used jQuery UI to make it easier to address Drag operations.

References:

huangapple
  • 本文由 发表于 2023年6月8日 02:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定