CSS Concept: position: pointer;

In this example below you will see how to do a CSS Concept: position: pointer; with some HTML / CSS and Javascript

A new CSS position property type concept that sets an element position according to the pointer position. This position type is similar to the 'absolute' type but instead of being preset, its 'top','left' values are dynamically changing according to pointer movements (touch positions on touch-interfaces) in all directions or only horizontal or vertical directions.

Thumbnail
This awesome code was written by kindofone, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright kindofone ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>CSS Concept: position: pointer;</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <style>
      /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */
      button {
  font-family: monospace;
  font-size: 20px;
  display: block;
  position: pointer;
  top: 50%;
  left: 50%;
  outline: none;
  width: 280px;
  height: 280px;
  margin: -140px -140px;
  border: 0;
  border-radius: 100%;
  opacity: 0.85;
  background-color: #f977a3;
  *zoom: 1;
  filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFF977A3', endColorstr='#FFD9666F');
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjAuMCIgeDI9IjAuNSIgeTI9IjEuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2Y5NzdhMyIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2Q5NjY2ZiIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
  background-size: 100%;
  background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #f977a3), color-stop(100%, #d9666f));
  background-image: -moz-linear-gradient(top, #f977a3 0%, #d9666f 100%);
  background-image: -webkit-linear-gradient(top, #f977a3 0%, #d9666f 100%);
  background-image: linear-gradient(to bottom, #f977a3 0%, #d9666f 100%);
  -moz-appearance: none;
  -webkit-appearance: none;
  box-shadow: inset 0px 1px 1px white, 0px 1px 2px rgba(0, 0, 0, 0.3);
}
button > label, button > span {
  cursor: inherit;
}
button > span {
  font-size: 13px;
}
button:hover {
  background-color: #f7afc7;
  *zoom: 1;
  filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFF7AFC7', endColorstr='#FFD9666F');
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjAuMCIgeDI9IjAuNSIgeTI9IjEuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2Y3YWZjNyIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2Q5NjY2ZiIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
  background-size: 100%;
  background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #f7afc7), color-stop(100%, #d9666f));
  background-image: -moz-linear-gradient(top, #f7afc7 0%, #d9666f 100%);
  background-image: -webkit-linear-gradient(top, #f7afc7 0%, #d9666f 100%);
  background-image: linear-gradient(to bottom, #f7afc7 0%, #d9666f 100%);
}
button:active {
  box-shadow: 0px 1px 1px white, inset 0px 1px 2px rgba(0, 0, 0, 0.3);
  background-color: #d9666f;
  *zoom: 1;
  filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFD9666F', endColorstr='#FFF7AFC7');
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjAuMCIgeDI9IjAuNSIgeTI9IjEuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2Q5NjY2ZiIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2Y3YWZjNyIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
  background-size: 100%;
  background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #d9666f), color-stop(100%, #f7afc7));
  background-image: -moz-linear-gradient(top, #d9666f 0%, #f7afc7 100%);
  background-image: -webkit-linear-gradient(top, #d9666f 0%, #f7afc7 100%);
  background-image: linear-gradient(to bottom, #d9666f 0%, #f7afc7 100%);
}

mark {
  background: none;
  font-weight: bold;
  text-decoration: underline;
}

body {
  background: #222;
  position: absolute;
  width: 100%;
  height: 100%;
}

    </style>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>

</head>

<body>

  <button data-position="pointer">
  <label>position: pointer;</label><br />
  <span>
    <mark>p</mark>ointer
    pointer-<mark>x</mark>, 
    pointer-<mark>y</mark>
    Click to switch states
  </span>
</button>

<!--
Author: Yogev Ahuvia © All Rights Reserved
##########################################
A new CSS position property type concept that sets an element
position according to the pointer position while hovering.
On touch-based interface, the pointer position could be the
point(s) of touch.
The new position type supports 3 values:
'pointer': the element will follow pointer
'pointer-x': the element will follow the pointer horizontaly
'pointer-y': the element will follow the pointer verticaly
-->
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/kindofone/css-concept-position-pointer-AapBy */
button {
  font-family: monospace;
  font-size: 20px;
  display: block;
  position: pointer;
  top: 50%;
  left: 50%;
  outline: none;
  width: 280px;
  height: 280px;
  margin: -140px -140px;
  border: 0;
  border-radius: 100%;
  opacity: 0.85;
  background-color: #f977a3;
  *zoom: 1;
  filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFF977A3', endColorstr='#FFD9666F');
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjAuMCIgeDI9IjAuNSIgeTI9IjEuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2Y5NzdhMyIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2Q5NjY2ZiIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
  background-size: 100%;
  background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #f977a3), color-stop(100%, #d9666f));
  background-image: -moz-linear-gradient(top, #f977a3 0%, #d9666f 100%);
  background-image: -webkit-linear-gradient(top, #f977a3 0%, #d9666f 100%);
  background-image: linear-gradient(to bottom, #f977a3 0%, #d9666f 100%);
  -moz-appearance: none;
  -webkit-appearance: none;
  box-shadow: inset 0px 1px 1px white, 0px 1px 2px rgba(0, 0, 0, 0.3);
}
button > label, button > span {
  cursor: inherit;
}
button > span {
  font-size: 13px;
}
button:hover {
  background-color: #f7afc7;
  *zoom: 1;
  filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFF7AFC7', endColorstr='#FFD9666F');
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjAuMCIgeDI9IjAuNSIgeTI9IjEuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2Y3YWZjNyIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2Q5NjY2ZiIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
  background-size: 100%;
  background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #f7afc7), color-stop(100%, #d9666f));
  background-image: -moz-linear-gradient(top, #f7afc7 0%, #d9666f 100%);
  background-image: -webkit-linear-gradient(top, #f7afc7 0%, #d9666f 100%);
  background-image: linear-gradient(to bottom, #f7afc7 0%, #d9666f 100%);
}
button:active {
  box-shadow: 0px 1px 1px white, inset 0px 1px 2px rgba(0, 0, 0, 0.3);
  background-color: #d9666f;
  *zoom: 1;
  filter: progid:DXImageTransform.Microsoft.gradient(gradientType=0, startColorstr='#FFD9666F', endColorstr='#FFF7AFC7');
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjAuMCIgeDI9IjAuNSIgeTI9IjEuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2Q5NjY2ZiIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iI2Y3YWZjNyIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
  background-size: 100%;
  background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #d9666f), color-stop(100%, #f7afc7));
  background-image: -moz-linear-gradient(top, #d9666f 0%, #f7afc7 100%);
  background-image: -webkit-linear-gradient(top, #d9666f 0%, #f7afc7 100%);
  background-image: linear-gradient(to bottom, #d9666f 0%, #f7afc7 100%);
}

mark {
  background: none;
  font-weight: bold;
  text-decoration: underline;
}

body {
  background: #222;
  position: absolute;
  width: 100%;
  height: 100%;
}


/*Downloaded from https://www.codeseek.co/kindofone/css-concept-position-pointer-AapBy */
$(document).on('mousemove', function(e) {
  $('[data-position="pointer"]').css('top', e.pageY).css('left', e.pageX);
  $('[data-position="pointer-x"]').css('left', e.pageX);
  $('[data-position="pointer-y"]').css('top', e.pageY);
});

$('[data-position^=pointer]').css('position', 'absolute');

$(document).on('mouseenter', function() {
  $(window).focus();
});

$(document).on('mouseleave', function() {
  $(window).blur();
  $('.item').css({'top':'50%', 'left':'50%'});
});

function switchPositionType($element, type) {
  $element.attr('data-position', type);
  $('button > label').text('position: '+type+';');
}

$('button').on('click', function() {
  $('[data-position^=pointer]').each(function(index, element) {
    if ($(this).attr('data-position') == 'pointer')
      switchPositionType($(this), 'pointer-x');
    else if ($(this).attr('data-position') == 'pointer-x')
      switchPositionType($(this), 'pointer-y');
    else if ($(this).attr('data-position') == 'pointer-y')
      switchPositionType($(this), 'pointer');
  });
});

$(window).on('keydown', function(e) {
  switch (e.keyCode) {
    case 80: // 'p'
      switchPositionType($('button'), 'pointer');
      break;
    case 88: // 'x'
      switchPositionType($('button'), 'pointer-x');
      break;
    case 89: // 'y'
      switchPositionType($('button'), 'pointer-y');
      break;
  }
});

Comments