jquery - Javascript draggable div background -


i got little problem, tried modify code 1 day.. without success.



problem that: draggable works good, can drag without problem image.. goes further image make me see blue of background color.

i know right problem bounds, don't know to.. resolve problem, tried grab x , y image without success.

$(document).ready(function(){ var $bg = $('.bg-img'),     data = $('#data')[0],     elbounds = {         w: parseint($bg.width()),          h: parseint($bg.height())     },     bounds = {w: 2350 - elbounds.w, h: 1750 - elbounds.h},     origin = {x: 0, y: 0},     start = {x: 0, y: 0},     movecontinue = false;  function move (e){     var inbounds = {x: false, y: false},         offset = {             x: start.x - (origin.x - e.clientx),              y: start.y - (origin.y - e.clienty)         };      data.value = 'x: ' + offset.x + ', y: ' + offset.y;      inbounds.x = offset.x < 0 && (offset.x * -1) < bounds.w;     inbounds.y = offset.y < 0 && (offset.y * -1) < bounds.h;      if (movecontinue && inbounds.x && inbounds.y) {         start.x = offset.x;         start.y = offset.y;          $(this).css('background-position', start.x + 'px ' + start.y + 'px');     }      origin.x = e.clientx;     origin.y = e.clienty;      e.stoppropagation();     return false; }  function handle (e){     movecontinue = false;     $bg.unbind('mousemove', move);      if (e.type == 'mousedown') {         origin.x = e.clientx;         origin.y = e.clienty;         movecontinue = true;         $bg.bind('mousemove', move);     } else {         $(document.body).focus();     }      e.stoppropagation();     return false; }  function reset (){     start = {x: 0, y: 0};     $(this).css('backgroundposition', '0 0'); }  $bg.bind('mousedown mouseup mouseleave', handle); $bg.bind('dblclick', reset); }); 

example code: https://jsfiddle.net/zt1jjzql/3/

here modify couple of things;

  • new function calculate image dimensions
  • calculate left , image can move.
  • limit movement , left those.

$(document).ready(function() {    var $bg = $('.bg-img'),      data = $('#data')[0],      elbounds = {        w: parseint($bg.width()),        h: parseint($bg.height())      },      bounds = {        w: 2350 - elbounds.w,        h: 1750 - elbounds.h      },      origin = {        x: 0,        y: 0      },      start = {        x: 0,        y: 0      },      movecontinue = false;    bgsize($bg, function(w, h) { //act on , store , left      console.log(`image dimensions => width:${w}, height:${h}`);      $bg.data("mostleft", (elbounds.w * -1) + w);      $bg.data("mostup", (elbounds.h * -1) + h);    });        function move(e) {      var inbounds = {          x: false,          y: false        },        offset = {          x: start.x - (origin.x - e.clientx),          y: start.y - (origin.y - e.clienty)        };        data.value = 'x: ' + offset.x + ', y: ' + offset.y;        inbounds.x = offset.x < 0 && (offset.x * -1) < bounds.w;      inbounds.y = offset.y < 0 && (offset.y * -1) < bounds.h;        if (movecontinue && inbounds.x && inbounds.y) {        // ensure , left limited appropriately        start.x = offset.x < ($bg.data("mostleft") * -1) ? ($bg.data("mostleft") * -1) : offset.x;        start.y = offset.y < ($bg.data("mostup") * -1) ? ($bg.data("mostup") * -1) : offset.y;          $(this).css('background-position', start.x + 'px ' + start.y + 'px');      }        origin.x = e.clientx;      origin.y = e.clienty;        e.stoppropagation();      return false;    }      function handle(e) {      movecontinue = false;      $bg.unbind('mousemove', move);        if (e.type == 'mousedown') {        origin.x = e.clientx;        origin.y = e.clienty;        movecontinue = true;        $bg.bind('mousemove', move);      } else {        $(document.body).focus();      }        e.stoppropagation();      return false;    }      function reset() {      start = {        x: 0,        y: 0      };      $(this).css('backgroundposition', '0 0');    }      $bg.bind('mousedown mouseup mouseleave', handle);    $bg.bind('dblclick', reset);  });  //function accurately calculate image size.  function bgsize($el, cb) {    $('<img />')      .load(function() {        cb(this.width, this.height);      })      .attr('src', $el.css('background-image').match(/^url\("?(.+?)"?\)$/)[1]);  }
div.bg-img {    background-image: url(http://i.imgur.com/zcdmwnx.gif);    background-position: 0 0;    background-repeat: no-repeat;    background-color: blue;    border: 1px solid #aaa;    width: 500px;    height: 250px;    margin: 25px auto;  }    p,  #data {    text-align: center;  }    #data {    background: red;    font-weight: bold;    color: white;    padding: 5px;    font-size: 1.4em;    border: 1px solid #ddd;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="bg-img"></div>  <p>    <input type="text" id="data" />  </p>


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -