javascript jquery在水平滚动容器中查找元素的左侧位置
我有一个溢出:自动容器,其跨文档窗口宽度的400%.因此,我的页面上有一个水平滚动条.我在此容器内还有多个div,这些容器的左侧位置不同.单击它们时,我需要获取每个容器的左侧位置.我使用$(this).offset().left,但这给了我容器div的左偏移量,即0px,我使用了$(this).position().left,但是给了我同样的东西. ..有什么建议吗?
I have a overflow:auto container that spans 400% width of the document window. Therefore I have a horizontal scrollbar on my page. I also have multiple div's inside this container with different left positions. I need to get the left position of each container as I click on them. I use $(this).offset().left but that gives me the left offset of the container div which is 0px and I've used $(this).position().left but that gives me the same thing.... any suggestions?
标记看起来像这样:
<div id='scroll'>
<div id='content'>
<div class='container' rel='1'></div>
<div class='container' rel='2'></div>
<div class='container' rel='3'></div>
<div class='container' rel='4'></div>
</div>
</div>
css
#scroll{
position:absolute;
width:100%;
height:95%;
overflow:auto;
}
#content{
float:left;
height:100%;
}
.container{
height:100%;
float:left;
}
jquery
var iMaxSize = $(".container").size();
$("#content").css({width: $(document).width()*iMaxSize +'px' });
$(".container").css({width: $("#content").width()/iMaxSize +'px' });
由于某种原因,我仍然无法使.offset()正常工作,也无法使scrollLeft()工作.我只是决定以一种非常全面的方式进行此操作.
Since for some reason I still can't get .offset() to work the way it should, or scrollLeft(). I just decided to do this a very round about way.
$('.container').click(function(){
var num = parseInt( $(this).attr('rel') );
var left = $('.container').width() * num-1;
var top = $('.container').css('top');
//do something with these values
});