Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <div 
     class="focal-picker" 
     data-image="http://www.amazingwallpaperz.com/wp-content/uploads/Photography-Widescreen-Background-Wallpapers.jpg" 
     data-position="35% 65%"
     data-desktop-width="100%"
     data-desktop-height="800px"
     data-tablet-width="1024px"
     data-tablet-height="500px"
     data-mobile-width="350px"
     data-mobile-height="450px"
     >
</div>
              
            
!

CSS

              
                * {
  max-width: 100%;
}
body {
  height: 100%;
  width: 100%;
  background: #eaeaea;
  font-family: "Roboto";
  color: #424242;
  width: 100vw;
}


.js-focal-picker {
  // Container for background image
.focal-background-container {
  height: 100%;
  width: 100%;
  background-position: 0% 0%;
  background-repeat: no-repeat;
  background-color: transparent;
  background-size:cover;
  will-change: background-position;
}

// Container for picker
.focal-positioner-container {
  position: absolute;
  bottom: 3em;
  left: 3em;
  background: #fafafa;
  width: 20em;
  max-width: 45vw;
  box-shadow: 0 2px 5px rgba(0,0,0,.26);
  z-index: 10;
  text-align: center;
  padding: 0 0.5em 0.25em;
  box-sizing: border-box;
}
// Title
.focal-picker-title {
  text-align: left;
  margin: 12px 0;
  font-size: 14px;
  font-weight: bold;
}
.focal-picker {
  position: relative;
}
// Blue point
.focal-point {
  background-color: cyan;
  width: 1em;
  height: 1em;
  opacity: 0.7;
  top: 0; 
  left: 0;
  position: absolute;
  border-radius: 50%;
  cursor: move;
  &:before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
  }
}


.focal-viewport {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 1100px;
  height: 750px;
  transition: all 0.2s cubic-bezier(0.4, 0.0, 0.6, 1);
  transform: translate3d(-50%,-50%,0);
  will-change: width, height;
  box-shadow: 0 2px 5px rgba(0,0,0,.26);
  max-width: 95%;
  margin: 0 auto;
}

.focal-controls {
  text-align: left;
  padding-right: 0.25em;
  color: #666;
}
.focal-results {
  font-size: 12px;
  margin: 12px 0;
  display: block;
  font-weight: bold;
}
.focal-control {
  font-size: 1.25em;
  display: inline-block;
  margin: 0 0.25em;
  vertical-align: sub;
  cursor: pointer;
  color: #000;
  opacity: 0.38;
  &.active {
    opacity: 0.60;
  }
}
}
              
            
!

JS

              
                ( function ($) {
  
  $.fn.focalPicker = function ( options ) {
    
    var settings = $.extend({
      currentPosition: '50% 50%',
      image: '',
      desktopWidth: '1100px',
      desktopHeight: '750px',
      tabletWidth: '768px',
      tabletHeight: '1024px',
      mobileWidth: '350px',
      mobileHeight: '550px'
    }, options);
        
    
    return this.each(function() {
      var $this = $(this);
      
      // Update setting for this element from it's data attributes
      settings.image = $this.attr('data-image');
      settings.currentPosition = $this.attr('data-position');
      var coords = settings.currentPosition.split(' ');
      settings.coords = {
        x: coords[0],
        y: coords[1]
      };
      
      // Update sizing of viewport if desktop/mobile deminsions specified
      if (typeof $this.attr('data-desktop-width') !== typeof undefined && $this.attr('data-desktop-width') !== false) {
        settings.desktopWidth = $this.attr('data-desktop-width');
      }
      if (typeof $this.attr('data-desktop-height') !== typeof undefined && $this.attr('data-desktop-height') !== false) {
        settings.desktopHeight = $this.attr('data-desktop-height');
      }
      if (typeof $this.attr('data-tablet-height') !== typeof undefined && $this.attr('data-tablet-height') !== false) {
        settings.tabletWidth = $this.attr('data-tablet-width');
      }
       if (typeof $this.attr('data-tablet-width') !== typeof undefined && $this.attr('data-tablet-width') !== false) {
        settings.tabletHeight = $this.attr('data-tablet-height');
      } 
      if (typeof $this.attr('data-mobile-height') !== typeof undefined && $this.attr('data-mobile-height') !== false) {
        settings.mobileWidth = $this.attr('data-mobile-width');
      }
       if (typeof $this.attr('data-mobile-width') !== typeof undefined && $this.attr('data-mobile-width') !== false) {
        settings.mobileHeight = $this.attr('data-mobile-height');
      } 
      
      
      
      // Variable to hold all of our dom element/variables for this picker
      var Focal = {};
      // The current picker element
      Focal.pickerElm = $(this);
      
      
      embedHTML();
       
      
      /**
        Embed needed HTML below the element
      **/
      function embedHTML() {
        var pickerHTML = `
  <div class="js-focal-picker">
    <div class="focal-viewport" style="width:`+settings.desktopWidth+`; height:`+settings.desktopHeight+`";>
        <div class="focal-background-container" style="background-image:url(`+settings.image+`);background-position:`+settings.currentPosition+`"></div>
    </div>
    <br>
    <br>
    <div class="focal-positioner-container">
        <p class="focal-picker-title">Choose background image focal point:</p>
        <div class="focal-picker" id="focal-picker"><img class="focal-img" src="`+settings.image+`">
            <div class="focal-point ui-widget-content"></div>
        </div>
        <div class="focal-controls">
          <span class="focal-results">Position: `+settings.currentPosition+`</span>
          <span data-type="focal-desktop" class="focal-control active">
            <i class="icon-desktop"></i>
          </span>
          <span data-type="focal-tablet" class="focal-control">
            <i class="icon-tablet"></i>
          </span>
          <span data-type="focal-mobile" class="focal-control">
            <i class="icon-mobile-phone"></i>
          </span>
        </div>
    </div>
    <input class="background-position-input" type="hidden" name="background_position" value="`+settings.currentPosition+`">
</div>`;  
        Focal.pickerElm.html(pickerHTML);
        
        initVariables();
      }
      
      
      /**
        Initialize variables for each picker
      **/
      function initVariables() {
        Focal.picker = Focal.pickerElm.find($('.focal-img'));
        Focal.point = Focal.pickerElm.find($('.focal-point'));
        Focal.background = Focal.pickerElm.find($('.focal-background-container'));
        Focal.controls = Focal.pickerElm.find($(".focal-control"));
        Focal.viewport = Focal.pickerElm.find($('.focal-viewport'));
        Focal.results = Focal.pickerElm.find($('.focal-results'));
        Focal.position = Focal.pickerElm.attr('data-position');
        Focal.x = settings.coords.x;
        Focal.y = settings.coords.y;
        Focal.formInput = Focal.pickerElm.find($('.background-position-input'));

        // Initialize placement of point for current image
        Focal.point.css({
            left: Focal.x,
            top: Focal.y
        });
        
        
        setEventListeners();
      }
      
      
      
      /**
        Set event listeners for created elements
      **/
      function setEventListeners() { 
        // Dragging dot
        Focal.point.draggable({
            cursor: "move",
            drag: function(e) {
              var x = Math.round( (e.target.offsetLeft - 24) / Focal.picker.width() * 100);
              var y = Math.round( (e.target.offsetTop - 24) / Focal.picker.height() * 100);
              if (x < 0) { x = 0; }
              if (y < 0) { y = 0; }
              Focal.x = x;
              Focal.y = y;
              updateResults();
            },
            containment: Focal.pickerElm.find('.focal-picker')
        });
        
        // Click viewport icon
        Focal.controls.on('click', changeViewport); 
      }
      
      
      /**
       Toggle viewport size
       **/
      function changeViewport()  {
          var type = $(this).attr('data-type');
        
          if (type === 'focal-desktop') {
            Focal.viewport.css({
              width: settings.desktopWidth,
              height: settings.desktopHeight
            });
           } 
          else if (type === 'focal-tablet') {
            Focal.viewport.css({
              width: settings.tabletWidth,
              height: settings.tabletHeight
            });
          }
          else if (type === 'focal-mobile') {
            Focal.viewport.css({
              width: settings.mobileWidth,
              height: settings.mobileHeight
            });
          }
        
          Focal.controls.removeClass('active');
        
          $(this).addClass('active'); 
      }
      
      
      /**
        Update results tag and input
      **/
      function updateResults() {
        Focal.results.text('Position: ' + Focal.x + '% ' + Focal.y + "%");
        Focal.background.css('background-position', Focal.x + "% " + Focal.y + "%");
        Focal.formInput.val(Focal.x + '% ' + Focal.y + '%');
      }

      
    }); // end .each()
    
    
  }
  
}( jQuery ));







/**

FULL SPEED, CAPTAIN!!!!

**/

$('.focal-picker').focalPicker();
              
            
!
999px

Console