React - make slider dragable
Starting point for creating JSFiddles with React. This uses React with Addons.
by STHayden
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://fb.me/react-with-addons-0.14.6.js"></script>
<script src="https://fb.me/react-dom-0.14.6.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<h1>
Make this slider draggable so it stays in the gray background
</h1>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
CSS
body {
padding: 10px;
}
.toggle-switch {
border-radius: 4px;
background-color: #ccc;
position: relative;
width: 450px;
height: 50px;
margin: 10px 0;
}
.toggle-switch-knob {
position: absolute;
width: 50px;
height: 50px;
border-radius: 4px;
background-color: rgb(130, 181, 198);
}
JavaScript 1.7
const { number } = React.PropTypes
const ToggleSwitch = React.createClass({
propTypes: {
animationDuration: number
},
getDefaultProps() {
return {
animationDuration: 350
}
},
getInitialState() {
return {
knobLeft: 0
}
},
toggle() {
this.setState({
knobLeft: this.state.knobLeft === 0 ? 400 : 0
})
},
handleClick() {
this.toggle()
},
render() {
const knobStyle = {
WebkitTransform: `translate3d(${this.state.knobLeft}px,0,0)`,
transform: `translate3d(${this.state.knobLeft}px,0,0)`
}
console.log(1111)
return (
<div className="toggle-switch" onClick={this.handleClick}>
<div className="toggle-switch-knob" style={knobStyle} />
</div>
)
}
})
ReactDOM.render(
<ToggleSwitch />,
document.getElementById('container')
);