<输入类型='文本区域' onmousemove=“toscheck()” >>仅适用于 Firefox

发布于 2024-10-08 12:41:53 字数 596 浏览 10 评论 0 原文

由于某种原因,此代码适用于所有 chrome、safari 和 ei,但不适用于 Firefox。

        <script type="text/javascript">
        function toscheck(){
        if(tos.scrollTop+540 > tos.scrollHeight){
            alert(tos.scrollTop + " " + tos.scrollHeight);
        }
    }
        </script>

----------

    <textarea name="tos" id="tos" readonly="readonly"  onmousemove="toscheck()">text</textarea>

在 FF 上不会触发任何内容,但在所有其他浏览器上工作正常。

但是, text 工作正常。

我是 javascript 新手,所以任何帮助将不胜感激。

For some reason, this code works all chrome, safari, and ei but not on firefox.

        <script type="text/javascript">
        function toscheck(){
        if(tos.scrollTop+540 > tos.scrollHeight){
            alert(tos.scrollTop + " " + tos.scrollHeight);
        }
    }
        </script>

----------

    <textarea name="tos" id="tos" readonly="readonly"  onmousemove="toscheck()">text</textarea>

Nothing's triggered on FF but works fine on all other browsers.

However, <textarea.. onmousemove=alert('test')>text</textarea> works fine.

I'm new to javascript so any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薯片软お妹 2024-10-15 12:41:53

这实际上不应该在任何浏览器中工作。您的 Javascript 代码中缺少右括号:

<script type="text/javascript">
function toscheck() {
  if (tos.scrollTop + 540 > tos.scrollHeight) {
    alert(tos.scrollTop + " " + tos.scrollHeight);
  }
}
</script>

That should actually not work in any browser. There is a closing bracket missing in your Javascript code:

<script type="text/javascript">
function toscheck() {
  if (tos.scrollTop + 540 > tos.scrollHeight) {
    alert(tos.scrollTop + " " + tos.scrollHeight);
  }
}
</script>
微凉徒眸意 2024-10-15 12:41:53

Live example

除了您缺少的 } 之外,以关闭您的函数看来我在FF工作。

<script type="text/javascript">
    function toscheck(){
        if(tos.scrollTop+540 > tos.scrollHeight){
            alert(tos.scrollTop + " " + tos.scrollHeight);
        }
    } // <----- was missing
</script>

另外,在您的函数中,您可以直接转到tos.property。

您需要将 this 传递到 mousemove="toscheck(this)" 并进行如下所示的函数设置:

<script type="text/javascript">
    function toscheck(elem){
        if(elem.scrollTop+540 > elem.scrollHeight){
            alert(elem.scrollTop + " " + elem.scrollHeight);
        }
    }
</script>

或者从函数中获取文本区域,如下所示:

<script type="text/javascript">
    function toscheck(){
        var tos = document.getElementById('tos');
        if(tos.scrollTop+540 > tos.scrollHeight){
            alert(tos.scrollTop + " " + tos.scrollHeight);
        }
    }
</script>

Live example

Other than your missing } to close out your function it seems to work in FF for me.

<script type="text/javascript">
    function toscheck(){
        if(tos.scrollTop+540 > tos.scrollHeight){
            alert(tos.scrollTop + " " + tos.scrollHeight);
        }
    } // <----- was missing
</script>

Also, in your function you directly go to tos.property.

You'll either need to pass this into the mousemove="toscheck(this)" and have your function setup like this:

<script type="text/javascript">
    function toscheck(elem){
        if(elem.scrollTop+540 > elem.scrollHeight){
            alert(elem.scrollTop + " " + elem.scrollHeight);
        }
    }
</script>

Or get your textarea from within the function like this:

<script type="text/javascript">
    function toscheck(){
        var tos = document.getElementById('tos');
        if(tos.scrollTop+540 > tos.scrollHeight){
            alert(tos.scrollTop + " " + tos.scrollHeight);
        }
    }
</script>
り繁华旳梦境 2024-10-15 12:41:53

最好的解决方案,使用 jQuery,它是自动跨浏览器方法,或者参见此页面了解如何使代码交叉- 由于 re:

Scrolling offset - how much the page has scrolled.

var x,y;
if (self.pageYOffset) // all except Explorer
{
    x = self.pageXOffset;
    y = self.pageYOffset;
}
else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
{
    x = document.documentElement.scrollLeft;
    y = document.documentElement.scrollTop;
}
else if (document.body) // all other Explorers
    {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }

和中的实现之间的差异,浏览器兼容

var x,y;
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight
    if (test1 > test2) // all but Explorer Mac
    {
        x = document.body.scrollWidth;
        y = document.body.scrollHeight;
    }
    else // Explorer Mac;
         //would also work in Explorer 6 Strict, Mozilla and Safari
    {
        x = document.body.offsetWidth;
        y = document.body.offsetHeight;
    }

Best solution, use jQuery and it's automatically cross-browser method or see this page regarding making that code cross-browser compatible due to differences between implementation in re:

Scrolling offset - how much the page has scrolled.

var x,y;
if (self.pageYOffset) // all except Explorer
{
    x = self.pageXOffset;
    y = self.pageYOffset;
}
else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
{
    x = document.documentElement.scrollLeft;
    y = document.documentElement.scrollTop;
}
else if (document.body) // all other Explorers
    {
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }

and

var x,y;
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight
    if (test1 > test2) // all but Explorer Mac
    {
        x = document.body.scrollWidth;
        y = document.body.scrollHeight;
    }
    else // Explorer Mac;
         //would also work in Explorer 6 Strict, Mozilla and Safari
    {
        x = document.body.offsetWidth;
        y = document.body.offsetHeight;
    }
如果没有你 2024-10-15 12:41:53

您的实际文本框有多大?也许它的渲染尺寸与您预期的不同和/或滚动高度有点不同?可能是字体大小/分辨率/DPI 设置问题。要尝试解决此问题,请添加少量重叠量,这样您实际上就不必滚动到绝对底部(在 IE 中不起作用,例如使用键盘导航和 Ctrl + End 时)。

if(tos.scrollTop + 565 > tos.scrollHeight){

顺便说一句,我不会在 onmousemove 中检查这一点,因为人们也可能使用键盘按键进行导航。我建议使用 onblur 以及 onmouseup 和/或 onkeyup。但是,如果在滚动条上释放鼠标按钮,onmouseup 可能不会触发。

How big is your actual text box? Maybe it's rendered in a different size than you expect and/or the scrollheight is a little bit different? Maybe it's a font size/resolution/DPI setting issue. To try to solve it, add a small overlapping amount so you actually don't have to scroll to the absolute bottom (won't work in IE for example when using keyboard navigation and Ctrl + End).

if(tos.scrollTop + 565 > tos.scrollHeight){

On a side note, I wouldn't check this in onmousemove as people might use keyboard keys to navigate as well. I'd suggest using onblur and maybe onmouseup and/or onkeyup. onmouseup however might not fire if the mouse button is released while being on the scroll bar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文