Creating a Volume Controller with jQuery UI Slider
Step 2: HTML markup
The markup for the slider is very simple, we wrapped all necessary markup – the tooltip, the slider, and the volume – inside an HTML5section
tag. The jQuery UI will then automatically generate the rest.Step 3: Install the Slider UI
The snippet below will install the Slider on the page, but it applies only the default configuration.- $(function() {
- $( "#slider" ).slider();
- });
First, we store the slider element as a variable.
- var slider = $('#slider'),
- slider.slider({
- range: "min",
- value: 35,
- });
Step 4: The Styles
Before proceeding, we need some assets from the PSD source file such as the background texture and the icon.- Converting a Design from PSD to HTML – Nettuts+
We will start by positioning the slider at the center of the browser’s window and attach the background that we had sliced out from the PSD to the
body
.- body {
- background: url('../images/bg.jpg') repeat top left;
- }
- section {
- width: 150px;
- height: auto;
- margin: 100px auto 0;
- position: relative;
- }
Slider
Since we did not define the maximum value for the Slider itself, the jQuery UI will apply the default; that is 100. Therefore, we can also apply 100 (px) for the slider’swidth
.- #slider{
- border-width: 1px;
- border-style: solid;
- border-color: #333 #333 #777 #333;
- border-radius: 25px;
- width: 100px;
- position: absolute;
- height: 13px;
- background-color: #8e8d8d;
- background: url('../images/bg-track.png') repeat top left;
- box-shadow: inset 0 1px 5px 0px rgba(0, 0, 0, .5),
- 0 1px 0 0px rgba(250, 250, 250, .5);
- left: 20px;
- }
Tooltip
The tooltip will be positioned above the slider by specifying its position absolutely with-25px
for its top position.- .tooltip {
- position: absolute;
- display: block;
- top: -25px;
- width: 35px;
- height: 20px;
- color: #fff;
- text-align: center;
- font: 10pt Tahoma, Arial, sans-serif ;
- border-radius: 3px;
- border: 1px solid #333;
- box-shadow: 1px 1px 2px 0px rgba(0, 0, 0, .3);
- box-sizing: border-box;
- background: linear-gradient(top, rgba(69,72,77,0.5) 0%,rgba(0,0,0,0.5) 100%);
- }
Volume
We have modified the volume icon a bit to meet our idea. The idea is we are going to create an effect to raise the volume bar gradually in accordance with the slider’s value. I’m sure you often seen such an effect in a media player user interface.The UI Handle
The handle’s style is quite simple; it will have an icon that looks like a metallic circle, sliced from the PSD, and attached as the background.We will also change the cursor mode to
pointer
, so the user will notice that this element is drag-able.- .ui-slider-handle {
- position: absolute;
- z-index: 2;
- width: 25px;
- height: 25px;
- cursor: pointer;
- background: url('../images/handle.png') no-repeat 50% 50%;
- font-weight: bold;
- color: #1C94C4;
- outline: none;
- top: -7px;
- margin-left: -12px;
- }
The Slider Range
The slider range will have a slightly white gradient color.- .ui-slider-range {
- background: linear-gradient(top, #ffffff 0%,#eaeaea 100%);
- position: absolute;
- border: 0;
- top: 0;
- height: 100%;
- border-radius: 25px;
- }
Step 5: The Effect
In this step we are going to start working on the Slider’s special effect.Tooltip
At this point, the tooltip has no content yet, so we are going to fill it with the slider’s value. Also, the tooltip’s horizontal position will be correspond with the handle’s position.First of all, we store the tooltip element as a variable.
- var tooltip = $('.tooltip');
- slide: function(event, ui) {
- var value = slider.slider('value'),
- tooltip.css('left', value).text(ui.value);
- tooltip.hide();
- start: function(event,ui) {
- tooltip.fadeIn('fast');
- },
- stop: function(event,ui) {
- tooltip.fadeOut('fast');
- },
Volume
As we have mentioned above in the Styles section, we plan the volume icon to change correspondingly with the handle’s position or to be exact, the slider’s value. So, we will apply the following conditional statement to create this effect.But, firstly, we store the volume element as well as the slider’s value as a variable.
- volume = $('.volume');
When the slider’s value is lower or equal to 5 the volume icon will have no bars at all, indicating that the volume is very low, but when the slider’s value is increasing, the volume bar will start increasing as well.
- if(value <= 5) {
- volume.css('background-position', '0 0');
- }
- else if (value <= 25) {
- volume.css('background-position', '0 -25px');
- }
- else if (value <= 75) {
- volume.css('background-position', '0 -50px');
- }
- else {
- volume.css('background-position', '0 -75px');
- };
Put them all together
All right, in case you are confused with all the above stuff, don’t be. Here is the shortcut. Put all the following codes into your document.- $(function() {
- var slider = $('#slider'),
- tooltip = $('.tooltip');
- tooltip.hide();
- slider.slider({
- range: "min",
- min: 1,
- value: 35,
- start: function(event,ui) {
- tooltip.fadeIn('fast');
- },
- slide: function(event, ui) {
- var value = slider.slider('value'),
- volume = $('.volume');
- tooltip.css('left', value).text(ui.value);
- if(value <= 5) {
- volume.css('background-position', '0 0');
- }
- else if (value <= 25) {
- volume.css('background-position', '0 -25px');
- }
- else if (value <= 75) {
- volume.css('background-position', '0 -50px');
- }
- else {
- volume.css('background-position', '0 -75px');
- };
- },
- stop: function(event,ui) {
- tooltip.fadeOut('fast');
- },
- });
- });
Conclusion
Today we have successfully created a more elegant jQuery UI theme but at the same time we have also successfully translated a PSD User Interface into a functional volume controller.We hope this tutorial inspires you and could help you understand how to turn a PSD into a more usable product.
Lastly, if you have any question regarding this tutorial, feel free to add it in the comments section below.
source
http://www.hongkiat.com/blog/jquery-volumn-slider/
Aucun commentaire:
Enregistrer un commentaire