当前位置: 首页 > 面试题库 >

jQuery scrollTop()在Safari或Chrome(Windows)中似乎不起作用

曾皓
2023-03-14
问题内容

我有一个简单的设置,可以加载“帮助”样式的窗口并将其滚动到页面上的特定点。或多或少的代码看起来像这样:

var target = /* code */;
target.offsetParent().scrollTop(target.offset().top - fudgeValue);

滚动的目标和软糖的值由放置在页面上的几个提示确定,我对该机制的任何部分都没有问题。在Firefox和IE8中,上述代码的工作方式与我想要的完全一样:滚动框(在本例中为页面正文)在被告知这样做时,会将包含的内容正确滚动到窗口中的正确点。

但是,在Chrome和Safari中,对scrollTop()的调用显然没有任何作用。所有的数字都可以,目标指向正确的事物(offsetParent()确实是body元素),但是什么也没有发生。据我从谷歌搜索中得知,这应该是可行的。Safari和Chrome下的渲染器是否有趣?

如果这很重要,则为jQuery 1.3.2。


问题答案:

是的,Chrome似乎在修改body,试图使其变成时存在错误offsetParent。作为一个变通办法,我建议你只需添加另一个div来包裹#contentDIV,并使该滚动:

html, body { height: 100%; padding: 0; } 
html { width: 100%; background-color: #222; overflow: hidden; margin: 0; } 
body 
{ 
   width: 40em; margin: 0px auto; /* narrow center column layout */
   background-color: white; 
   position: relative; /* allow positioning children relative to this element */
} 
#scrollContainer /* wraps #content, scrolls */
{ 
  overflow: auto; /* scroll! */
  position:absolute; /* make offsetParent */
  top: 0; height: 100%; width: 100%; /* fill parent */
} 
#header 
{ 
  position: absolute; 
  top: 0px; height: 50px; width: 38.5em; 
  background-color: white; 
  z-index: 1; /* sit above #content in final layout */
} 
#content { padding: 5px 14px 50px 5px;  }

在FF 3.5.5,Chrome 3.0.195.33,IE8中测试

现场演示:

$(function() {

        $('#header').find('button').click(function(ev) {

          var button = $(this), target = $('div.' + button.attr('class'));

          var scroll = target.offsetParent().scrollTop();

          target.offsetParent().scrollTop(target.offset().top + scroll - 50);

        });

      });


html, body { height: 100%; padding: 0; }

      html { width: 100%; background-color: #222; overflow: hidden; margin: 0; }

      body { width: 40em; margin: 0px auto; background-color: white; position: relative; }

      #scrollContainer { overflow: auto; position:absolute; top: 0; height: 100%; width: 100%; }

      #header { position: absolute; top: 0px; height: 50px; width: 38.5em; background-color: white; z-index: 1; }

      #content { padding: 5px 14px 50px 5px;  }


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    <div id='header'>

      Header Box

      <button class='A'>A</button>

      <button class='B'>B</button>

      <button class='C'>C</button>

    </div>

    <div id='scrollContainer'>

    <div id='content'>

      <div style='height: 50px'> </div>

      <div class='A'>

        <h1>A</h1>

        <p>My name is Boffer Bings. I was born of honest parents in one of the humbler walks of life, my father being a manufacturer of dog-oil and my mother having a small studio in the shadow of the village church, where she disposed of unwelcome babes. In my boyhood I was trained to habits of industry; I not only assisted my father in procuring dogs for his vats, but was frequently employed by my mother to carry away the debris of her work in the studio. In performance of this duty I sometimes had need of all my natural intelligence for all the law officers of the vicinity were opposed to my mother's business. They were not elected on an opposition ticket, and the matter had never been made a political issue; it just happened so. My father's business of making dog-oil was, naturally, less unpopular, though the owners of missing dogs sometimes regarded him with suspicion, which was reflected, to some extent, upon me. My father had, as silent partners, all the physicians of the town, who seldom wrote a prescription which did not contain what they were pleased to designate as _Ol. can._ It is really the most valuable medicine ever discovered. But most persons are unwilling to make personal sacrifices for the afflicted, and it was evident that many of the fattest dogs in town had been forbidden to play with me--a fact which pained my young sensibilities, and at one time came near driving me to become a pirate.

      </div>

      <div class='B'>

        <h1>B</h1>

        <p>

        Looking back upon those days, I cannot but regret, at times, that by indirectly bringing my beloved parents to their death I was the author of misfortunes profoundly affecting my future.

        <p>

        One evening while passing my father's oil factory with the body of a foundling from my mother's studio I saw a constable who seemed to be closely watching my movements. Young as I was, I had learned that a constable's acts, of whatever apparent character, are prompted by the most reprehensible motives, and I avoided him by dodging into the oilery by a side door which happened to stand ajar. I locked it at once and was alone with my dead. My father had retired for the night. The only light in the place came from the furnace, which glowed a deep, rich crimson under one of the vats, casting ruddy reflections on the walls. Within the cauldron the oil still rolled in indolent ebullition, occasionally pushing to the surface a piece of dog. Seating myself to wait for the constable to go away, I held the naked body of the foundling in my lap and tenderly stroked its short, silken hair. Ah, how beautiful it was! Even at that early age I was passionately fond of children, and as I looked upon this cherub I could almost find it in my heart to wish that the small, red wound upon its breast--the work of my dear mother--had not been mortal.

      </div>

      <div class='C'>

        <h1>C</h1>

        <p>It had been my custom to throw the babes into the river which nature had thoughtfully provided for the purpose, but that night I did not dare to leave the oilery for fear of the constable. "After all," I said to myself, "it cannot greatly matter if I put it into this cauldron. My father will never know the bones from those of a puppy, and the few deaths which may result from administering another kind of oil for the incomparable _ol. can._ are not important in a population which increases so rapidly." In short, I took the first step in crime and brought myself untold sorrow by casting the babe into the cauldron.

      </div>

      <div style='height: 75em;'> </div>

    </div>

    </div>


 类似资料:
  • 我的代码看起来像 我的文件如下所示 当我运行程序时,我看到 我怎样才能修好它呢?

  • 问题内容: 我在使用该功能时遇到了麻烦。 我只需要知道SQL查询是否返回零行。 我已经尝试过以下简单的语句: 类型是哪里。上面的代码似乎不起作用。无论是否为空,它将始终打印该消息。 我检查了SQL查询本身,当存在行时它正确返回了非空结果。 关于如何确定查询是否已返回0行的任何想法?我用谷歌搜索,找不到任何答案。 问题答案: ResultSet.getFetchSize()不返回结果数!从这里: 使

  • 我在Windows上,我已经在环境变量中设置了JDK 1.8的路径。同样在项目结构方面,我已经改变了。我有一个Maven项目。 在命令行中,如果我使用“mvn--version”,我会看到 然而,在IntelliJ中,如果我点击Run按钮,它总是指向JDK 1.7,并抛出一个错误(因为我的maven项目需要java 8)。我该怎么改变呢?谢谢 更新在遵循@BevyQ提示后,我发现Maven run

  • 问题内容: 即使将属性设置为,我仍然会插入重复的条目。 我设置了使用定期在。我没有用表 问题答案: 我没有使用JPA创建表 然后,您应该在语句中向表中添加唯一约束,例如,如果您使用的是MySQL:

  • 根据我所发现和看到的一切,这似乎是正确的。打印$query时,结果如下所示: “在客户(名字、名字、姓氏、地址、城市、州、邮政编码、电子邮件、性别)中插入值(?,,,,,,,,,,?,,,?)” 参数应该已经用bindValues()中的变量填充。所以,举个例子。。。 插入到客户(第一名、中间名、最后名、地址、城市、州、邮编、电子邮件、性别)值(比尔、霍普金斯、123大街、...) 我想继续使用这

  • 下面的方法有2个for循环。Firstloop迭代36000次,inner forloop迭代24次,因此记录插入的总数将为864000。代码执行运行了将近3.5小时并终止。(记录插入失败)只有在outer for循环结束后才会关闭会话并提交事务。发现执行过程中RAM消耗近6.9GB 我已经附上了最后几行GC日志来指示内存分配 堆PSYoungGen总计1862144K,已使用889367K[0x