初期サイズを取得する必要があるサイズ変更可能なバーが複数ありますが、要素が未定義であることを取得し続けます。これまでの私のコードは次のとおりです。
<div class="r_bar"><span class="resizable" style="width: 34%;"><div class="marker"><p></p></div></span></div>
...そして、私が Eskimo の助けを借りて取り組んできた jQuery です。
$(function(){
var initialWidth = $('.r_bar').siblings().find('span').css('width').replace('%', '');
initialWidth = parseInt(initialWidth);
var dragging = false;
$('.resizable').mousedown(function(e){
dragging = true;
var parentOffset = $(this).parent().offset();
$this = $(e.currentTarget);
e.preventDefault();
$(document).mousemove(function(e){
if (dragging == true){
$this.css("width",e.pageX - parentOffset.left);
var percentageChange = (e.pageX - parentOffset.left) / initialWidth * 100
$('.marker p').html(percentageChange.toFixed(0));
}
});
});
$(document).mouseup(function(e){
if (dragging){
dragging = false;
}
});
});
現在これが行っていることは、バーがドラッグされていない場合でも、バーのすべてのインスタンスの数値を更新することです。また、ページの読み込み時に initialWidth も表示されません。助言がありますか?