【问题标题】:Having trouble the actual width of some html in javascript在javascript中遇到一些html的实际宽度问题
【发布时间】:2014-02-06 18:57:03
【问题描述】:

我正在尝试使用 javascript 函数 width 来获取容器的宽度,并使用它来计算一些滚动文本应该开始滚动的位置(我希望它在黄色框内滚动):

现在,javascript 代码如下:

alert(yellowBox.width());
alert(yellowBox.innerWidth());
alert(yellowBox.outerWidth());

全部显示,944,但我确定黄色框的宽度实际上是 500,但我不想使用该值,因为宽度可能会改变。

而且我不确定如何获得框的实际宽度。我试过使用yellowBox.offsetWidth()yellowBox.clientWidth,但它们不起作用,Web Developer, Web Console plugin for firefox 告诉我“yellowBox.offsetWidth 不是一个函数”,还有“yellowBox.clientWidth不是函数。

这是我的代码:

html:

<div class="container-fluid" style="padding: 5px 20px;">

  <div class="well" style="background-color: <?php echo $layout_setting[2][value]; ?>; font-size:large; font-weight:bold;">

    <div class="marquee" style="white-space: nowrap; width: 100%; color: <?php echo $layout_setting[7][value] ?>; overflow: hidden; ">

      <?php echo $rssContent; ?>

    </div>
  </div>

</div>

js插件代码:

(function( $ ) {
    $.fn.marquee = function(params) {
        params = $.extend( {direction : 'left',duration : '2000', delayStart : '0'}, params);
        var duration = parseInt(params.duration);
        var delay = parseInt(params.delayStart);

        var par = $(this);

        alert(par.width());
        alert(par.innerWidth());
        alert(par.outerWidth());

        par.wrapInner('<span></span>');

        /*I get the same result for width both before and after the line that says `par.wrapInner('<span></span>');*/
        alert(par.width());
        alert(par.innerWidth());
        alert(par.outerWidth());

        var parCh = par.children('span');
        var leftMargin = parCh.css('margin-left').replace('px', '');
        var rightMargin = par.innerWidth() - leftMargin - parCh.width();

        function dirRight() {
            parCh.css({'margin-left' : '' + leftMargin + 'px'});
            //I've determined that `500` is about the width of the yellow box, by changing the line above to:
            //`parCh.css({'margin-left' : '' + leftMargin + 500 + 'px'});`
            //and seeing where that is the about the value it needs to be to have the left side of the text start out scrolling into the right side of the box
            //instead of having the left side of the scrolling text starting at the left side of the yellow box
            parCh.animate({'margin-left' : '' + rightMargin + 'px'}, duration, 'linear', function() { dirRight(); });
        }

        function dirLeft() {
            parCh.css({'margin-left' : '' + rightMargin + 'px'});
            parCh.animate({'margin-left' : '' + leftMargin + 'px'}, duration, 'linear', function() { dirLeft(); });
        }

        if (params.direction == 'right') setTimeout(function(){ dirRight() }, delay);

        if (params.direction == 'left') {
            parCh.css({'margin-left' : '' + rightMargin + 'px'});
            setTimeout(function(){ dirLeft() }, delay);
        }

        $(window).resize(function() { rightMargin = par.innerWidth() - leftMargin - parCh.width(); });
    };
}( jQuery ));

以及我调用插件的函数:

function scrollMarquee() {
    setInterval("scrollMarquee()", 1000000);

$('.marquee').marquee({'direction' : 'right', 'delayStart' : '0', 'duration' : '1000000'});

}

【问题讨论】:

  • 你能放一个 jsfiddle 来演示这个问题吗?到目前为止,真的很难为你所拥有的东西提供帮助。
  • 不会在控制台中工作,因为它不知道 yelowbox 是什么。你是怎么确定宽度是500的?还添加一些相关的 html/css 以便我们知道这个框发生了什么
  • 你的 html 中没有 marquee 的类。
  • 我真的不明白你是如何得到宽度“真的”500px的,但一方面你不需要在你的div上继续调用marqueemarquee 代码中已经存在超时。
  • 删除由 Colin 评论的 setInterval

标签: javascript jquery html actualwidth


【解决方案1】:

原因可能是您在错误的 div 或元素上使用了 width() innerWidth()outerWidth()

<div id="div1">
 <div id="div2" style="height:100px;width:300px;background-color:lightblue;"></div>
</div>
<script>
$(document).ready(function(){
    alert("Width of outer div: " + $("#div1").width()+"="+   $("#div1").innerWidth()+"="+ $("#div1").outerWidth());
    alert("Width of inner div: " + $("#div2").width()+"="+   $("#div2").innerWidth()+"="+ $("#div2").outerWidth());
});
</script>

会给你不同的价值

【讨论】:

  • 我不认为我使用了错误的 div -- 我已经更新了问题,提供了更多详细信息,显示了我的相关代码并解释了我如何知道框宽度约为 500。
  • 这可能很愚蠢,但您的带有well 类的 div 是否从某个地方获得宽度 944?因为那么你的选框 div 将得到 100%
  • Yogesh 我认为在警告宽度时 div 的宽度为 100%,然后在执行内联 CSS 时将其更改为更小的宽度
  • @Zword,我不确定,但代码使用的是 bootstrap.css:getbootstrap.com
  • 当您将选取框包裹在 span 中时,父项的宽度可能会发生变化。我想到的是尝试修复父元素的宽度。
【解决方案2】:

尝试以下方法:

$(window).load(function(){
    $('.marquee').marquee({'direction' : 'right', 'delayStart' : '0', 'duration' : '1000000'});
});

$(document).ready(function(){
    $('.marquee').marquee({'direction' : 'right', 'delayStart' : '0', 'duration' : '1000000'});
});

【讨论】:

  • 对不起,这是问题中的错误,我的 html 代码确实包含 .marquee 类,我已经更新了问题以表明这一点。
  • 每当我提醒宽度显示正确的宽度时,您的 PHP 内容是否超出屏幕宽度?
  • 好吧,如果我这样做 alert(parCh.width()),它会显示 44168,因为 $rssContent 的值是一个很长的不同新闻故事标题列表,但这不是我的值用于尝试获取黄色框的宽度,我正在尝试par.width()par.innerWidth()par.outerWidth(),所有这些都给出了944 的值,即使正确的宽度约为500
  • 可以添加页面截图吗?
  • 我去掉了setInterval,添加了网页截图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 2014-04-15
相关资源
最近更新 更多