Coder Social home page Coder Social logo

bar.js's Introduction

bar.js

Create a draggable bar that could be used as range selector / progress bar / scroll bar or others.

Examples

<div id="bar"></div>
<script src="bar.min.js"></script>
<script>
	let bar = new Bar(document.querySelector('#bar'), {
		inner_mode: true
	});
	bar.elem.fill.style.cssText = 'background: red';
	bar.render();
	bar.on('change', value => {
		console.log(value);
	});
</script>

Click DEMO to try it!

Init options

let bar = new Bar(container, options);
  • container (required, type: HTMLElement)

  • options (optional, type: Object)

    • default (type: Number, default: 0): initial value of bar

    • precision (type: Number, default: 2): number of digits after the decimal point

    • disabled (type: Boolean, default: false): whether value can be set by click/drag

    • inner_mode (type: Boolean, default: false): whether dot can overflow wrapper

Properties

bar.elem; // returns {dot, fill, wrapper}, all of them are HTMLElement.

bar.value; // return current value;
bar.value = 0.6; // set value to 0.6;

Methods

  • render()

    Render bar.

  • on(event, callback)

    Bind callback to event.

    events:

    • change : callback(value, type)
  • enable()

    Enable bar evnets(click and drag).

  • disable()

    Disable bar events(click and drag).

  • hide_dot()

    Hide dot Element.

  • show_dot()

    Show dot Element.

  • hide_fill()

    Hide fill Element.

  • show_fill()

    Show fill Element.

Tips

  • After new Bar(container), the DOM structure would become:

      <div>                      // container
      	<div>				   // wrapper
      		<div></div>		   // fill
      		<div></div>		   // dot
      	</div>
      </div>
    
  • We need to set dot/fill style manully because it doesn't has default style.

    We could operate it as native DOM element, for example:

      bar1.elem.dot.className = 'my-dot';
      bar2.elem.dot.style.background = 'red';
      bar3.elem.dot.style.cssText = 'background: red';
    
  • Remember to execute render() after you update dot/fill style!

  • Bar could be used on 3 typical scenarios:

    1. range selector

       let bar = new Bar(container);
       bar.elem.dot.style.cssText = 'width: 20px; height: 20px; background: red';
      
    2. progress bar

       let bar = new Bar(container, {
       	disabled: true
       });
       bar.elem.fill.style.cssText = 'background: red';
      
    3. scroll bar

       let bar = new Bar(container, {
       	inner_mode: true
       });
       bar.elem.dot.style.cssText = 'width: 20px; height: 100%; background: red';
      

bar.js's People

Contributors

nossika avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.