[英]How to track the percentage of the bar while resizing in real time
I have a progress bar that acts more like a bar chart. 我有一个进度条,其行为更像条形图。 I pull the initial width using JSON and have it to where the bar is resizable to the right. 我使用JSON拉出初始宽度,并将其放到右边可调整大小的位置。 previous work I'm trying to track and display the changing percentages as the bar is dragged either left or right. 以前的工作中,我尝试跟踪和显示随栏向左或向右拖动而变化的百分比。 My attempts so far have not been fruitful using jquery ui. 到目前为止,我使用jquery ui的尝试还没有取得成果。 Does anyone have an example or suggestion I could use to figure this out? 有没有人可以举例说明或提出建议?
http://jsfiddle.net/z9E5U/2/ http://jsfiddle.net/z9E5U/2/
I'm gonna show you a vanilla jQuery way of doing it (no jQuery UI). 我将向您展示一种普通的jQuery方式(没有jQuery UI)。 First up, we create our html elements: 首先,我们创建html元素:
<div class="dragwrapper" style="margin: auto; width: 100px">
<div style="width: 120px; height: 20px; background-color:#F00; cursor: w-resize" class="dragme"></div>
</div>
<div class="result">0%</div>
So we have a wrapper with a class .dragwrapper
. 因此,我们有一个带有.dragwrapper
类的包装器。 This is used to be the reference to which the mouse is moving. 这曾经用作鼠标移动的参考。 Then we have our actual draggable element with a class .dragme
and a few css attributes to make it visible. 然后,我们有一个带有类.dragme
和一些CSS属性的实际可拖动元素,以使其可见。 I am using an initial size of 120px in this example, but the percentage can easily be calculated regardless of the initial size 在此示例中,我使用的初始尺寸为120px,但是无论初始尺寸如何,都可以轻松计算百分比
For out javascript (make sure you have jQuery loaded): 对于javascript(确保已加载jQuery):
$(function(){
var initialWidth = 120;
var dragging = false;
$('.dragme').mousedown(function(e){
dragging = true;
var parentOffset = $(this).parent().offset();
e.preventDefault();
$(document).mousemove(function(e){
if (dragging == true){
$('.dragme').css("width",e.pageX - parentOffset.left);
var percentageChange = (e.pageX - parentOffset.left) / initialWidth * 100
$('.result').html(percentageChange + "%");
}
});
});
$(document).mouseup(function(e){
if (dragging){
dragging = false;
console.log('drag stopped');
}
});
});
So, we are first declaring a variable called dragging
which is initially set to false
, as well as a variable holding our initial width. 因此,我们首先要声明一个称为dragging
的变量,该变量最初设置为false
,以及一个保留我们初始宽度的变量。 We then listen for a mousedown
event on the .dragme
element. 然后,我们在.dragme
元素上监听mousedown
事件。 We set dragging
to true
, get the offset of the wrapper and do a preventDefault
for good measure. 我们将dragging
设置为true
,获取包装器的偏移量,并执行preventDefault
以获得良好的效果。 We then follow the mouse movement and change the css of the .dragme
element 然后,我们跟随鼠标移动并更改.dragme
元素的css
We also listen for a mouseup
event to stop the dragging if dragging
variable is true
如果dragging
变量为true
我们还将监听mouseup
事件以停止拖动
You compare the offset ( e.pageX - parentOffset.left
) with the initial width to calculate the percentage 您将偏移量( e.pageX - parentOffset.left
)与初始宽度进行比较,以计算百分比
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.