This is a rundown of tasks that I am struggling to code more effectively:
- When the bar is clicked anywhere, I want the black dot button to instantly move there and automatically update the displayed percentage below it.
- Additionally, when I drag the button to the desired location and release it outside the bar div, the percentage below does not refresh. How can I fix this?
Check out the relevant code snippets:
<script src="jQueryUI/external/jquery/jquery.js"></script>
<script src="jQueryUI/jquery-ui.js"></script>
<script src="jQueryUI/jquery-ui.css"></script>
<script>
$(function() {
$( ".draggable" ).draggable({
axis: 'x', containment: [-234,0,450,0]
});
});
</script>
<script>
$( ".but1" ).click(function( event ) {
var offset = $( ".but1" ).offset();
event.stopPropagation();
var conta = (offset.left - 752)/3.42;
var posFinal = Math.round( conta );
$( "#resp1" ).text( posFinal + "%" );
});
</script>
.draggable {
width: 700px;
height: 0px;
margin: 0px auto;
position: relative;
}
#bar {
width: 350px;
height: 12px;
background-color: #ff0a00;
border-right: 350px solid #00b4ff;
margin: 0px auto;
border-radius: 5px;
}
#circle {
position: relative;
top: -15px;
width: 18px;
height: 18px;
border-radius: 50%;
background: black;
margin: 0px auto;
}
#discordo {
position: relative;
top: 65px;
left: -386px;
font-weight: bold;
}
#concordo {
position: relative;
top: 31.8px;
left: 390px;
font-weight: bold;
}
#organizado {
position: relative;
top: -70px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="x.php" id="questions">
<h3>Pergunta 1</h3>
<p>Maconha deveria ser legalizada para uso recreacional</p>
<div id="organizado">
<p id="discordo">Discordo</p>
<p id="concordo">Concordo</p>
<div id="bar"> </div>
<div class="draggable ui-widget-content">
<div id="circle" class="but1"> </div>
</div>
<div>
</div>
<p id="resp1"></p>
For better visualization, here is an image: https://i.sstatic.net/9MmZG.png