Simple pure Javascript drag controller slider
Asked Answered
A

2

11

Hi Developers all over the world.

I would like to have some help with a simple, pure Javascript (30 lines), JQuery free (and other library's) drag control slider.

I have been searching months and found many scripts but i don't like -Jquery cause most script need 4, 5, 6 javascript includes.. I prefer smaller and basic script.. i like to ajust them like i want and i also can lean alot from smaller scripts.

All i need is a simple slider that i can use for: rescale images, scroll page, change brightness on images (with PHP) etc.

I am new with javascript (2 months), this is how far i get now.. Sorry for the bad variable names...


    <script type="text/javascript">  
      _item = null;
      mouse_x = 0;
      drag_x = 0; 
      function move_init() {
        document.onmousemove = _move;
        document.onmouseup = _stop;
      }
      function _stop(){
         _item = null;
      }
      function _move(e){
        mouse_x = document.all ? window.event.clientX : e.pageX;
        if(_item != null){
             _item.style.left = (mouse_x - drag_x) + "px";
        }
      }
      function _move_item(drag)
      {
        _item = drag;
        drag_x = mouse_x - _item.offsetLeft;
      }
move_init();
drag.onmousedown=_move_item();   // Agh.. did'nt figure out how this works
</script>

<style type="text/css">
#drag{background:#797979;color:#fff;width:30px;height:15px;position:relative;}
#track{background:red; width:200px;}
</style>

<div id="track"><div id="drag" onmousedown="_move_item(this);" >x</div></div>

i appriciate your help.

I wrote this on 31 december 2012.. So happy new year. Please be good for each other.

Thank you.

Acuminate answered 30/12, 2012 at 23:46 Comment(5)
Hi, @user1938684, if you're looking for a code review, please post this on codereview.stackexchange.com. Otherwise, if you have a specific question or problem with this code, feel free to state exactly what that is. As it stands, it's not clear exactly what kind of help you need.Hydrostat
I'll be happy if someone have a pice of code for me.. My code is not pasted for review... more for verify what i'm searching for. please give me a code.. then i'm happy and continue my development.Acuminate
Out of curiosity did you find a code? I'm looking for the same, still without result.Kuska
Possible duplicate of Is there a simple JavaScript slider?Hula
For new developers stumbling on this, use an input type range, let the browser do the work for you: <input type="range" name="points" min="0" max="10"> Preview: w3schools.com/tags/tryit.asp?filename=tryhtml5_input_type_rangeMagenmagena
J
27

This code works in modern browsers. Just create some polyfill for that addEventListener and this custom range slider will be safe to use:

function rangeSlider(id, onDrag) {

    var range = document.getElementById(id),
        dragger = range.children[0],
        draggerWidth = 10, // width of your dragger
        down = false,
        rangeWidth, rangeLeft;

    dragger.style.width = draggerWidth + 'px';
    dragger.style.left = -draggerWidth + 'px';
    dragger.style.marginLeft = (draggerWidth / 2) + 'px';

    range.addEventListener("mousedown", function(e) {
        rangeWidth = this.offsetWidth;
        rangeLeft = this.offsetLeft;
        down = true;
        updateDragger(e);
        return false;
    });

    document.addEventListener("mousemove", function(e) {
        updateDragger(e);
    });

    document.addEventListener("mouseup", function() {
        down = false;
    });

    function updateDragger(e) {
        if (down && e.pageX >= rangeLeft && e.pageX <= (rangeLeft + rangeWidth)) {
            dragger.style.left = e.pageX - rangeLeft - draggerWidth + 'px';
            if (typeof onDrag == "function") onDrag(Math.round(((e.pageX - rangeLeft) / rangeWidth) * 100));
        }
    }

}

Example Usage

<style>
.range-slider {
  width:300px;
  height:20px;
  margin:0 auto 1em;
  position:relative;
  cursor:e-resize;
}
.range-slider:before {
  content:"";
  display:block;
  position:absolute;
  top:9px;
  left:0;
  width:100%;
  height:2px;
  background-color:black;
}
.range-slider span {
  display:block;
  height:inherit;
  position:relative;
  z-index:2;
  background-color:red;
  cursor:inherit;
}
</style>

<div class="range-slider" id="range-slider-1">
  <span></span>
</div>

<script>
rangeSlider('range-slider-1', function(value) {
    document.getElementById('test-area').innerHTML = value + '%';
});
</script>

Demo: http://jsbin.com/dulifezi/2/edit


Update

I’m creating a repo for this snippet here → https://github.com/taufik-nurrohman/range-slider

Jointure answered 31/3, 2014 at 13:22 Comment(4)
Dragging doesn't seem to work on touch devices. Tested on an Android tablet.Egyptian
@Egyptian The GitHub repo version has support for touch events.Jointure
@TaufikNurrohman any idea how to move this code to typescript (angular)?Dispirit
@Dispirit Not an angular guy, sorry. No idea.Jointure
A
2

Take a look at DragDealer.js: https://skidding.github.io/dragdealer/

There's an example of changing opacity of an image based on the value of a slider here.

Hope this helps!

Awlwort answered 24/4, 2013 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.