Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Image Comparison Slider вешает браузер (https://javascript.ru/forum/jquery/69616-image-comparison-slider-veshaet-brauzer.html)

рони 06.07.2017 18:56

фото до и после с поддержкой мобильных touch-событий
 
:write:
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>demo</title>
  <style type='text/css'>
 html, body{
   height: 100%;
 }
.container {
    width: 50%;
    height: 38%;
     margin: 80px auto;
     position: relative;
}
.container > .top , .container > .bottom{
     height: 100%; width: 100%;
    position: absolute;
    top: 0;
    left: 0;
    overflow: auto;
    background-repeat: no-repeat;
    background-size: cover;
    background-image: url(http://s00.yaplakal.com/pics/pics_preview/6/1/9/9965916.jpg);
}

 .container > .bottom{
    background-image: url(http://s00.yaplakal.com/pics/pics_preview/7/1/9/9965917.jpg);
 }

.container > .bar {
    height: 100%;
    width: 3px;
    position: absolute;
    top: 0px;
    left: 50%;
    background: green;
    cursor: move;
}
  </style>



<script>
window.addEventListener("DOMContentLoaded", function() {
    var mousedown = "mousedown",
        mousemove = "mousemove",
        mouseup = "mouseup";
    var touch = !!("ontouchstart" in window);
    if (touch) mousedown = "touchstart", mousemove = "touchmove", mouseup = "touchend";
    [].forEach.call(document.querySelectorAll(".container"), function(container) {
        var bottom = container.querySelector(".bottom"),
            bar = container.querySelector(".bar"),
            left, h = container.clientHeight,
            w = container.clientWidth;
        var init = function(e) {
            e.preventDefault();
            touch && (e = e.changedTouches[0]);
            left = e.clientX - this.offsetLeft;
            h = container.clientHeight;
            w = container.clientWidth;
            clip(left);
            container.addEventListener(mousemove, move, true);
            window.addEventListener(mouseup, stop, true)
        };
        var move = function(e) {
            touch && (e = e.changedTouches[0]);
            var x = e.clientX - this.offsetLeft;
            x = Math.min(w, Math.max(0, x));
            clip(x)
        };
        var clip = function(left) {
            bottom.style.clip = "rect(0," + left + "px," + h + "px,0)";
            bar.style.left = left + "px"
        };
        clip(w / 2);
        var stop = function(e) {
            container.removeEventListener(mousemove, move, true);
            window.removeEventListener(mouseup,
                stop, true)
        };
        container.addEventListener(mousedown, init, true)
    })
});
</script>


</head>
<body>
  <div class="container">
    <div class="top"></div>
    <div class="bottom"></div>
    <div class="bar"></div>
</div>
 <div class="container">
    <div class="top" style="background-image: url(http://s00.yaplakal.com/pics/pics_preview/4/2/9/9965924.jpg)"></div>
    <div class="bottom" style="background-image: url(http://s00.yaplakal.com/pics/pics_preview/5/2/9/9965925.jpg)"></div>
    <div class="bar"></div>
</div>
</body>


</html>

j0hnik 06.07.2017 19:08

Рони, а под мобайл чтоб работало?

рони 06.07.2017 19:24

j0hnik,
искать надо :lol: это же самое но с touchend и touchmove touchstart

рони 06.07.2017 21:21

j0hnik,
добавил touch события -- протестируй, если не сложно
пост №11

j0hnik 06.07.2017 22:50

Рони, пашет все (по крайней мере Опера мобайл).
долго возился? если не секрет

рони 06.07.2017 23:48

Цитата:

Сообщение от j0hnik
долго возился?

Сегодня, 18:56
Последний раз редактировалось рони, Сегодня в 21:19.
2 часа прошло :) , а точно не знаю, алгоритм и touch уже делал, и где-то есть тут на форуме, написал по новой, в фоновом режиме.

рони 06.07.2017 23:55

j0hnik,
таки нашёл, если интересно
до и после, минимальная версия

j0hnik 07.07.2017 00:27

Цитата:

Сообщение от рони (Сообщение 457863)
j0hnik,
таки нашёл, если интересно
до и после, минимальная версия

может пригодится ) спасибо.


Часовой пояс GMT +3, время: 13:15.