Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery how to load part of image

I have a little problem. I need to create circular slider, I didn't find any library which could help me, so seems like I need to write it on my own.
Here is my problem:
enter image description here

I want to create something like in picture above, but I can't find a way to load part of that gradiant for example:

enter image description here

Is it even possible to load part of image in js?

like image 986
user1409508 Avatar asked Dec 12 '25 09:12

user1409508


2 Answers

If you do not need to replace the image dynamically, I would recommend to prepare it in an image tool such as Photoshop.

if you have few images to replace, I would prepare them in Photoshop and change them via a javascript and css solution

Otherwise I do not know.

like image 184
Bertrand Avatar answered Dec 13 '25 23:12

Bertrand


There is no way to load a part of an image in pure JavaScript. Instead you could use a sprite image and play with background position:

function getBgPos(pct) {
  if (pct < 12.5) { return '0 0'; }
  else if (pct < 25) { return '-100px 0'; }
  else if (pct < 37.5) { return '-200px 0'; }
  else if (pct < 50) { return '-300px 0'; }
  else if (pct < 62.5) { return '-400px 0'; }
  else if (pct < 75) { return '-500px 0'; }
  else if (pct < 87.5) { return '-600px 0'; }
  else if (pct < 100) { return '-700px 0'; }
  else { return '-800px 0'; }
};

i = 0;
setInterval(function () {
  jQuery('#loader').css('background-position', getBgPos(i));
  i += 12.5; if (i > 100) i = 0;
}, 500);
#loader {
  display: block;
  width: 100px;
  height: 100px;
  background: yellow url(https://i.sstatic.net/Xsf4k.png) no-repeat 0 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="loader" src="https://i.sstatic.net/ArcTX.png" />