cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="wrapper">

  <div class="image-holder">
    
    <img class="main-image" src="http://www.pseudosuede.com/2015running/images/map_background.png" alt="2015 Running Data Image.">
    
    <div class="relative-box">
      <div class="relative-locator">
      </div>
    </div>
    
  </div> <!-- .image-holder -->
  
  
  
</div>
<div class="text-feedback">
<p class="natural-img-width">Nat. Img. W 2880px</p>
<p class="natural-img-height">Nat. Img. H 1700px</p>
<p class="calculated-img-width"></p>
<p class="calculated-img-height"></p> 
<p class="img-shell-width"></p>
<p class="img-shell-height"></p> 
<p class="loc-box-top"></p>
<p class="loc-box-left"></p> 
<p class="loc-box-prcnt-left"></p>
<p class="loc-box-prcnt-top"></p>
<p class="img-drag-left"></p>
<p class="img-drag-top"></p>
<p class="img-stop-left"></p>
<p class="img-stop-top"></p>
<p class="img-prcnt-left"></p>
<p class="img-prcnt-top"></p>
</div>
            
          
!
            
              * { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; }

body {
  background-color:#DBD06C;
}

.text-feedback {
  position:absolute;
  top:5px;
  left:5px;
}

.wrapper {
  padding:10em;
  width:100%;
  margin:0 auto;
}

.image-holder {
  width:100%;
  height:600px;
  overflow:hidden;
  position:relative;
  background-color:rgba(0,0,0,0.10);
  img {
    display:block;
    position:relative;
    top:0;
    left:0;
    /*width:100%;*/ // Remove This to begin upper left image finder.
    cursor: -webkit-grab; cursor:-moz-grab;
      &:active {
        cursor: -webkit-grabbing; cursor:-moz-grabbing;
      }
  }
}

.relative-box {
  display:inline-block;
  position:absolute;
  top:1em;
  left:1em;
  width:200px;
  height:100px;
  background-color:rgba(0,0,0,.25);
  border-color:rgba(0,0,0,0.35);
  border-width:3px;
  border-style:solid;
}

.relative-locator {
  position:relative;
  top:0;
  left:0;
  width:50px;
  height:25px;
  background-color:rgba(255,255,255,.25);
  border-color:rgba(255,255,255,0.35);
  border-width:3px;
  border-style:solid;
  cursor: -webkit-grab; cursor:-moz-grab;
  &:active {
    cursor: -webkit-grabbing; cursor:-moz-grabbing;
  }
}
            
          
!
            
              // New Questions
// 1. Can I create a reusable function for the ideas in the .main-image draggable area. Perhaps I can use the traditional $().position instead of ui.position....
// 2. Do I need to split up the functions even more?

function getRelativeLocatorPosition() {
  var locBoxT = $('.relative-locator').position().top;
  var locBoxL = $('.relative-locator').position().left;

  $('p.loc-box-top').html("Loc. Top Value " + locBoxT + "px");
  $('p.loc-box-left').html("Loc. Left Value " + locBoxL + "px");

  return this;
}

function imageCalculation() {
  var calcImgW = $('.main-image').outerWidth();
  var calcImgH = $('.main-image').outerHeight();

  var calcShellW = $('.image-holder').outerWidth();
  var calcShellH = $('.image-holder').outerHeight();

  var imgProportion = calcImgH / calcImgW;
  var shellProportion = calcShellH / calcShellW;

  var relBoxW = $('.relative-box').outerWidth();
  var relBoxH = relBoxW * imgProportion;

  var locProportionW = calcShellW / calcImgW;
  var locProportionH = calcShellH / calcImgH;

  var borderThickness = 6; // For Each of the Boxes Borders

  var locBoxW = (relBoxW * locProportionW) - borderThickness;
  var locBoxH = (relBoxH * locProportionH) - borderThickness;
  var locBoxT = $('.relative-locator').position().top;
  var locBoxL = $('.relative-locator').position().left;

  var locBoxPercentageLeft = (locBoxL / (relBoxW - locBoxW - borderThickness)) * 100;
  var locBoxPercentageLeftRounded = Math.ceil(locBoxPercentageLeft);
  var locBoxPercentageTop = (locBoxT / (relBoxH - locBoxH - borderThickness)) * 100;
  var locBoxPercentageTopRounded = Math.ceil(locBoxPercentageTop);

  $('.relative-box').css("height", relBoxH);

  $('.relative-locator').css("width", locBoxW);
  $('.relative-locator').css("height", locBoxH);

  $('p.calculated-img-width').html("Calc. Img W " + calcImgW + "px");
  $('p.calculated-img-height').html("Calc. Img H " + calcImgH + "px");

  $('p.img-shell-width').html("Img Shell W " + calcShellW + "px");
  $('p.img-shell-height').html("Img Shell H " + calcShellH + "px");

  $('p.loc-box-prcnt-left').html("Loc. Left " + locBoxPercentageLeftRounded + "%");
  $('p.loc-box-prcnt-top').html("Loc. Top " + locBoxPercentageTopRounded + "%");

  return this;
}

$(function () {
  $('.relative-locator').draggable({
    containment: "parent",
    start: function() {
      getRelativeLocatorPosition();
      imageCalculation();
    },
    drag: function() {
      getRelativeLocatorPosition();
      imageCalculation();
    },
    stop: function() {
      getRelativeLocatorPosition();
      imageCalculation();
    }
  });
});

$(function() {
  $(".main-image").draggable({
    start: function() { },
    drag: function(event, ui) {
      var left = ui.position.left,
        offsetWidth = ($(this).width() - $(this).parent().width()) * -1,
        top = ui.position.top,
        offsetHeight = ($(this).height() - $(this).parent().height()) * -1;

      /*Begin Repeated Items, Fix If Successful*/
      /*var calcImgW = $('.main-image').outerWidth();
  var calcImgH = $('.main-image').outerHeight();

  var calcShellW = $('.image-holder').outerWidth();
  var calcShellH = $('.image-holder').outerHeight();*/
      /*End Repeated Items, Fix If Successful*/

      /*var imgBoxPercentLeft = (offsetWidth / (calcShellW - calcImgW)) * 100;
      var imgBoxPercentLeftRounded = Math.ceil(imgBoxPercentLeft);*/
      /*var locBoxPercentageTop = (locBoxT / (relBoxH - locBoxH - borderThickness)) * 100;
      var locBoxPercentageTopRounded = Math.ceil(locBoxPercentageTop);*/

      if (left > 0) {
        ui.position.left = 0;
      }
      if (offsetWidth > left) {
        ui.position.left = offsetWidth;
      }
      if (top > 0) {
        ui.position.top = 0;
      }
      if (offsetHeight > top) {
        ui.position.top = offsetHeight;
      }

      $('p.img-drag-left').html("Img. Drag Left " + offsetWidth);
      $('p.img-drag-top').html("Img. Drag Top " + offsetHeight);

      /*$('p.img-prcnt-left').html("Img. Loc. Left " + imgBoxPercentLeftRounded + "%");*/
      /*$('p.img-prcnt-top').html("Img. Loc. Top " + imgBoxPercentageTopRounded + "%");*/

    }, // drag: function
    stop: function(event, ui) {
     var left = ui.position.left
        var top = ui.position.top
        $('p.img-stop-left').html("Img. Stop Left " + left);
        $('p.img-stop-top').html("Img. Stop Top " + top);
      } // stop: function
  }); // .draggable
}); // function

$(window).load(function() {
  imageCalculation();
  getRelativeLocatorPosition();
});
$(window).resize(function() {
  imageCalculation();
  getRelativeLocatorPosition();
  // Function to make sure that when the image is dragged all the way to the right, and then the page is made wider, that the image also moves with it and the image wrapper background isn't exposed.
  // Function to make sure that when the Relative-Locator is in the lower right corner, and the screen is narrow and then dragged to a wider position, that the Relative-Location doesn't escape the Relative-Box.
});

// Research:

//http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome

//http://learn.jquery.com/plugins/basic-plugin-creation/
//http://stackoverflow.com/questions/15665231/run-jquery-function-on-window-events-load-resize-and-scroll
// -- Maybe I need to break down each individual task into it's own function so that I can call a specific class for each of the functions, like the above link shows.

//https://jqueryui.com/draggable/
//http://stackoverflow.com/questions/20635918/containment-issue-with-draggable-element-within-a-resizable-container
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console