“之前和之后”交互式图像蒙版

发布于 2024-12-05 05:56:19 字数 423 浏览 0 评论 0原文

我想使用 web-kit 重新创建最近在 Guardian.co.uk 上展示的前后图像滑块。

http://www.guardian.co.uk/uk/interactive/2011/aug/09/london-riots-before-after-photographs" Guardian.co.uk/uk/interactive/2011/aug/09/london-riots-before-after-photographs

它基本上是两个图像叠放在一起,带有垂直滑块它跟随鼠标,在左侧显示一张图像,在右侧显示另一张图像。

I'd like to recreate the before and after image slider recently featured on the guardian.co.uk, using web-kit.

http://www.guardian.co.uk/uk/interactive/2011/aug/09/london-riots-before-after-photographs

It's basically two images on top of each other with a vertical slider that follows the mouse, revealing one image on the left and the other on the right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

十秒萌定你 2024-12-12 05:56:19

我在此处提供了一个示例。它需要稍微扩展一下,但它显示了基本原理。它不使用 CSS3 的东西,因此,虽然不会因为使用 CSS3 而获得 GeekPoints(tm),但它可以在加载更多浏览器时工作。

HTML

<div class="reveal">
    <div>
        <span></span>
    </div>
</div>

jQuery

var handle = $("div.reveal div span");

$(document).ready(function() {
    handle.mousedown(function() {
        $(this).data("sliding", true);
    });
    handle.mouseup(function() {
        $(this).data("sliding", false);
    });
});

$(document).mousemove(function(e) {
    var img = $("div.reveal div");

    if(handle.data("sliding"))
    {
        var offs = e.pageX - img.offset().left
        img.width(offs);
    }
});

I've come up with an example here. It will need expanding a little, but it shows the basic principles. This doesn't use CSS3 stuff so, while not gaining GeekPoints(tm) for using CSS3, it'll work in loads more browsers.

HTML

<div class="reveal">
    <div>
        <span></span>
    </div>
</div>

jQuery

var handle = $("div.reveal div span");

$(document).ready(function() {
    handle.mousedown(function() {
        $(this).data("sliding", true);
    });
    handle.mouseup(function() {
        $(this).data("sliding", false);
    });
});

$(document).mousemove(function(e) {
    var img = $("div.reveal div");

    if(handle.data("sliding"))
    {
        var offs = e.pageX - img.offset().left
        img.width(offs);
    }
});
孤凫 2024-12-12 05:56:19

这是直接从监护人页面获取的代码:

ImageSlider = function (n, i, t) {
    var e = {};
    e.init = function () {
        e.container = n;
        e.beforeUrl = i;
        e.afterUrl = t;
        e.width = e.container.width();
        e.height = e.container.height();
        e.renderHtml();
        e.loadImages()
    };
    e.renderHtml = function () {
        var i = document.createDocumentFragment(),
            n = document.createElement("div");
        n.className = "imageMask";
        n.style.position = "absolute";
        n.style.overflow = "hidden";
        n.style.width = e.width + "px";
        n.style.height = e.height + "px";
        n.style.webkitBackfaceVisibility = "hidden";
        var y = document.createElement("div");
        y.style.position = "absolute";
        y.style.webkitBackfaceVisibility = "hidden";
        y.id = "before";
        var r = document.createElement("div");
        r.style.position = "absolute";
        r.style.webkitBackfaceVisibility = "hidden";
        r.id = "after";
        var w = document.createElement("div");
        w.className = "slider";
        w.style.position = "absolute";
        w.style.webkitBackfaceVisibility = "hidden";
        var t = document.createElement("div");
        t.style.left = "-2px";
        t.style.width = "4px";
        t.style.position = "absolute";
        t.style.height = e.height + "px";
        t.style.backgroundColor = "#FFF";
        t.style.boxShadow = "0 0 10px rgba(0, 0, 0, 1)";
        t.style.webkitBoxShadow = "0 0 10px rgba(0, 0, 0, 1)";
        t.style.MozBoxShadow = "0 0 10px rgba(0, 0, 0, 1)";
        var F = document.createElement("div");
        F.style.position = "absolute";
        F.style.width = e.width + "px";
        F.style.height = e.height + "px";
        n.appendChild(r);
        w.appendChild(t);
        i.appendChild(y);
        i.appendChild(n);
        i.appendChild(w);
        i.appendChild(F);
        e.container[0].style.position = "relative";
        e.container[0].style.overflow = "hidden";
        e.container.append(i.cloneNode(!0));
        e.preloader = e.container.children(".preloader");
        e.preloader[0].style.position = "absolute";
        e.preloader[0].style.top = (e.height - e.preloader.css("height").replace("px", "")) * 0.5 + "px";
        e.preloader[0].style.left = (e.width - e.preloader.css("width").replace("px", "")) * 0.5 + "px";
        e.handle = e.container.children(".slider");
        e.handleDisplay = e.handle.children();
        e.handleDisplay.hide();
        e.mask = e.container.children(".imageMask");
        e.before = e.container.children("#before");
        e.after = e.mask.children("#after")
    };
    e.loadImages = function () {
        e.imagesToLoad = 0;
        e.imagesLoaded = 0;
        e.loadImage(e.before, e.beforeUrl);
        e.loadImage(e.after, e.afterUrl)
    };
    e.loadImage = function (i, n) {
        e.imagesToLoad++;
        var t = new Image;
        $(t).load(function () {
            $(this).hide();
            $(i).removeClass("loading").append(this);
            e.imageLoaded()
        }).error(function () {}).attr("src", n)
    };
    e.imageLoaded = function () {
        e.imagesLoaded++;
        e.imagesLoaded >= e.imagesToLoad && e.allReady()
    };
    e.allReady = function () {
        e.setPosition(0, !0);
        e.before.children().fadeIn(500);
        e.after.children().fadeIn(500, e.finalise)
    };
    e.finalise = function () {
        e.dragging = !1;
        e.candrag = !0;
        e.lastXPos = 0;
        e.velocity = 0;
        Modernizr.touch && (document.addEventListener("touchstart", e.touchHandler, !0), document.addEventListener("touchmove", e.touchHandler, !0), document.addEventListener("touchend", e.touchHandler, !0), document.addEventListener("touchcancel", e.touchHandler, !0));
        e.container.mousedown(e.mouseDownHandler);
        e.container.mouseup(e.mouseUpHandler);
        e.container.mousemove(e.mouseMoveHandler);
        e.handleDisplay.fadeIn(250);
        e.preloader.remove();
        e.setPosition(e.width * 0.5, !1);
        Ticker.setFPS(30);
        Ticker.addListener(e)
    };
    e.tick = function () {
        if (e.dragging) e.lastXPos = e.handle.position().left;
        else if (e.velocity != 0) if (Math.abs(e.velocity) < 0.2) e.velocity = 0;
        else {
            e.velocity *= 0.9;
            var i = e.handle.position().left + e.velocity;
            if (i >= e.width) i = e.width, e.velocity = -e.velocity * 0.5;
            else if (i <= 0) i = 0, e.velocity = -e.velocity * 0.5;
            e.setPosition(i, !0)
        }
    };
    e.mouseDownHandler = function (i) {
        e.canDrag = !0;
        e.velocity = 0;
        e.setPosition(i.pageX - $(i.target).offset().left, !1);
        e.lastXPos = i.pageX - $(i.target).offset().left;
        i.preventDefault()
    };
    e.mouseUpHandler = function (i) {
        e.dragging ? (e.velocity = (e.handle.position().left - e.lastXPos) * 0.5, e.dragging = !1) : e.setPosition(i.pageX - $(i.target).offset().left, !1);
        e.canDrag = !1;
        i.preventDefault()
    };
    e.mouseMoveHandler = function (i) {
        if (e.canDrag) e.dragging = !0, e.setPosition(i.pageX - $(i.target).offset().left, !0);
        i.preventDefault()
    };
    e.setPosition = function (i, n) {
        var t = i - e.mask.width(),
            r = n ? 0 : 0.5;
        Modernizr.csstransforms && Modernizr.csstransitions ? (e.applyTransition(e.handle[0].style, "all " + r + "s ease-out 0s"), e.applyTransition(e.mask[0].style, "all " + r + "s ease-out 0s"), e.applyTransition(e.after[0].style, "all " + r + "s ease-out 0s"), / AppleWebKit\//.test(navigator.userAgent) ? (e.applyTransform(e.handle[0].style, "matrix(1,0,0,1," + i + ",0)"), e.applyTransform(e.mask[0].style, "matrix(1,0,0,1," + t + ",0)"), e.applyTransform(e.after[0].style, "matrix(1,0,0,1," + -t + ",0)")) : (e.applyTransform(e.handle[0].style, "matrix(1,0,0,1," + i + "px ,0)"), e.applyTransform(e.mask[0].style, "matrix(1,0,0,1," + t + "px ,0)"), e.applyTransform(e.after[0].style, "matrix(1,0,0,1," + -t + "px ,0)"))) : (e.mask.stop(), e.mask.animate({
            width: i
        }, r * 1E3), e.handle.stop(), e.handle.animate({
            left: i
        }, r * 1E3))
    };
    e.applyTransform = function (e, i) {
        e.transform = e.msTransform = e.OTransform = e.MozTransform = e.webkitTransform = i
    };
    e.applyTransition = function (e, i) {
        e.transition = e.msTransition = e.OTransition = e.MozTransition = e.webkitTransition = i
    };
    e.touchHandler = function (e) {
        var i = e.changedTouches[0],
            n = "";
        switch (e.type) {
        case "touchstart":
            n = "mousedown";
            break;
        case "touchmove":
            n = "mousemove";
            break;
        case "touchend":
            n = "mouseup";
            break;
        default:
            return
        }
        var r = document.createEvent("MouseEvent");
        r.initMouseEvent(n, !0, !0, window, 1, i.screenX, i.screenY, i.clientX, i.clientY, !1, !1, !1, !1, 0, null);
        i.target.dispatchEvent(r);
        e.preventDefault()
    };
    e.init();
    return {}
}

正如您所看到的,它非常混乱。第一个参数是 jQuery 对象,第二个和第三个参数是之前/之后的 url。

Here's the code taken straight from the guardian page:

ImageSlider = function (n, i, t) {
    var e = {};
    e.init = function () {
        e.container = n;
        e.beforeUrl = i;
        e.afterUrl = t;
        e.width = e.container.width();
        e.height = e.container.height();
        e.renderHtml();
        e.loadImages()
    };
    e.renderHtml = function () {
        var i = document.createDocumentFragment(),
            n = document.createElement("div");
        n.className = "imageMask";
        n.style.position = "absolute";
        n.style.overflow = "hidden";
        n.style.width = e.width + "px";
        n.style.height = e.height + "px";
        n.style.webkitBackfaceVisibility = "hidden";
        var y = document.createElement("div");
        y.style.position = "absolute";
        y.style.webkitBackfaceVisibility = "hidden";
        y.id = "before";
        var r = document.createElement("div");
        r.style.position = "absolute";
        r.style.webkitBackfaceVisibility = "hidden";
        r.id = "after";
        var w = document.createElement("div");
        w.className = "slider";
        w.style.position = "absolute";
        w.style.webkitBackfaceVisibility = "hidden";
        var t = document.createElement("div");
        t.style.left = "-2px";
        t.style.width = "4px";
        t.style.position = "absolute";
        t.style.height = e.height + "px";
        t.style.backgroundColor = "#FFF";
        t.style.boxShadow = "0 0 10px rgba(0, 0, 0, 1)";
        t.style.webkitBoxShadow = "0 0 10px rgba(0, 0, 0, 1)";
        t.style.MozBoxShadow = "0 0 10px rgba(0, 0, 0, 1)";
        var F = document.createElement("div");
        F.style.position = "absolute";
        F.style.width = e.width + "px";
        F.style.height = e.height + "px";
        n.appendChild(r);
        w.appendChild(t);
        i.appendChild(y);
        i.appendChild(n);
        i.appendChild(w);
        i.appendChild(F);
        e.container[0].style.position = "relative";
        e.container[0].style.overflow = "hidden";
        e.container.append(i.cloneNode(!0));
        e.preloader = e.container.children(".preloader");
        e.preloader[0].style.position = "absolute";
        e.preloader[0].style.top = (e.height - e.preloader.css("height").replace("px", "")) * 0.5 + "px";
        e.preloader[0].style.left = (e.width - e.preloader.css("width").replace("px", "")) * 0.5 + "px";
        e.handle = e.container.children(".slider");
        e.handleDisplay = e.handle.children();
        e.handleDisplay.hide();
        e.mask = e.container.children(".imageMask");
        e.before = e.container.children("#before");
        e.after = e.mask.children("#after")
    };
    e.loadImages = function () {
        e.imagesToLoad = 0;
        e.imagesLoaded = 0;
        e.loadImage(e.before, e.beforeUrl);
        e.loadImage(e.after, e.afterUrl)
    };
    e.loadImage = function (i, n) {
        e.imagesToLoad++;
        var t = new Image;
        $(t).load(function () {
            $(this).hide();
            $(i).removeClass("loading").append(this);
            e.imageLoaded()
        }).error(function () {}).attr("src", n)
    };
    e.imageLoaded = function () {
        e.imagesLoaded++;
        e.imagesLoaded >= e.imagesToLoad && e.allReady()
    };
    e.allReady = function () {
        e.setPosition(0, !0);
        e.before.children().fadeIn(500);
        e.after.children().fadeIn(500, e.finalise)
    };
    e.finalise = function () {
        e.dragging = !1;
        e.candrag = !0;
        e.lastXPos = 0;
        e.velocity = 0;
        Modernizr.touch && (document.addEventListener("touchstart", e.touchHandler, !0), document.addEventListener("touchmove", e.touchHandler, !0), document.addEventListener("touchend", e.touchHandler, !0), document.addEventListener("touchcancel", e.touchHandler, !0));
        e.container.mousedown(e.mouseDownHandler);
        e.container.mouseup(e.mouseUpHandler);
        e.container.mousemove(e.mouseMoveHandler);
        e.handleDisplay.fadeIn(250);
        e.preloader.remove();
        e.setPosition(e.width * 0.5, !1);
        Ticker.setFPS(30);
        Ticker.addListener(e)
    };
    e.tick = function () {
        if (e.dragging) e.lastXPos = e.handle.position().left;
        else if (e.velocity != 0) if (Math.abs(e.velocity) < 0.2) e.velocity = 0;
        else {
            e.velocity *= 0.9;
            var i = e.handle.position().left + e.velocity;
            if (i >= e.width) i = e.width, e.velocity = -e.velocity * 0.5;
            else if (i <= 0) i = 0, e.velocity = -e.velocity * 0.5;
            e.setPosition(i, !0)
        }
    };
    e.mouseDownHandler = function (i) {
        e.canDrag = !0;
        e.velocity = 0;
        e.setPosition(i.pageX - $(i.target).offset().left, !1);
        e.lastXPos = i.pageX - $(i.target).offset().left;
        i.preventDefault()
    };
    e.mouseUpHandler = function (i) {
        e.dragging ? (e.velocity = (e.handle.position().left - e.lastXPos) * 0.5, e.dragging = !1) : e.setPosition(i.pageX - $(i.target).offset().left, !1);
        e.canDrag = !1;
        i.preventDefault()
    };
    e.mouseMoveHandler = function (i) {
        if (e.canDrag) e.dragging = !0, e.setPosition(i.pageX - $(i.target).offset().left, !0);
        i.preventDefault()
    };
    e.setPosition = function (i, n) {
        var t = i - e.mask.width(),
            r = n ? 0 : 0.5;
        Modernizr.csstransforms && Modernizr.csstransitions ? (e.applyTransition(e.handle[0].style, "all " + r + "s ease-out 0s"), e.applyTransition(e.mask[0].style, "all " + r + "s ease-out 0s"), e.applyTransition(e.after[0].style, "all " + r + "s ease-out 0s"), / AppleWebKit\//.test(navigator.userAgent) ? (e.applyTransform(e.handle[0].style, "matrix(1,0,0,1," + i + ",0)"), e.applyTransform(e.mask[0].style, "matrix(1,0,0,1," + t + ",0)"), e.applyTransform(e.after[0].style, "matrix(1,0,0,1," + -t + ",0)")) : (e.applyTransform(e.handle[0].style, "matrix(1,0,0,1," + i + "px ,0)"), e.applyTransform(e.mask[0].style, "matrix(1,0,0,1," + t + "px ,0)"), e.applyTransform(e.after[0].style, "matrix(1,0,0,1," + -t + "px ,0)"))) : (e.mask.stop(), e.mask.animate({
            width: i
        }, r * 1E3), e.handle.stop(), e.handle.animate({
            left: i
        }, r * 1E3))
    };
    e.applyTransform = function (e, i) {
        e.transform = e.msTransform = e.OTransform = e.MozTransform = e.webkitTransform = i
    };
    e.applyTransition = function (e, i) {
        e.transition = e.msTransition = e.OTransition = e.MozTransition = e.webkitTransition = i
    };
    e.touchHandler = function (e) {
        var i = e.changedTouches[0],
            n = "";
        switch (e.type) {
        case "touchstart":
            n = "mousedown";
            break;
        case "touchmove":
            n = "mousemove";
            break;
        case "touchend":
            n = "mouseup";
            break;
        default:
            return
        }
        var r = document.createEvent("MouseEvent");
        r.initMouseEvent(n, !0, !0, window, 1, i.screenX, i.screenY, i.clientX, i.clientY, !1, !1, !1, !1, 0, null);
        i.target.dispatchEvent(r);
        e.preventDefault()
    };
    e.init();
    return {}
}

As you can see, it's quite a mess. The first argument is a jQuery object, and the second and third are before/after urls.

大姐,你呐 2024-12-12 05:56:19

Just in case anybody is still looking for a solution using webkit and CSS3 -- there's a demo here. Just change to "Position". This might not work in anything but Chrome.

汐鸠 2024-12-12 05:56:19

有趣的问题...

我想重新创建最近和之后的图像滑块,最近在guardian.co.uk上使用web-kit。

你为什么不使用原件?不需要jQuery!

监护人是Github上的“监护人”。他们知道该怎么做,所以这是开源的:
https://github.com/guardian/image-fader-slider

Funny question ...

I'd like to recreate the before and after image slider recently featured on the guardian.co.uk, using web-kit.

Why don't you use the original? There is no need for jQuery!

The Guardian is 'guardian' on github. They know how to do it and so it is OpenSource:
https://github.com/guardian/image-fader-slider

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文