简体   繁体   English

初始化 jquery 插件时设置 scrollTop

[英]Setting scrollTop when initializing jquery plugin

I have a jQuery plugin:我有一个 jQuery 插件:

http://jsfiddle.net/2kbrfv9z/ http://jsfiddle.net/2kbrfv9z/

The plugin works great, but I want to pass settings to the plugin so that I can set the ul on the right number.该插件效果很好,但我想将设置传递给插件,以便我可以将 ul 设置为正确的数字。

When I initialize the plugin and I want to set the start time on 2:00:00 I add a scroll top on the ul to set the right li.当我初始化插件并想将开始时间设置为 2:00:00 时,我在 ul 上添加了一个滚动顶部以设置正确的 li。 (ul with value 0 is scrollTop(0) and 1 is scrollTop(35)). (值为 0 的 ul 是 scrollTop(0),1 是 scrollTop(35))。 But I don't get it working.但我不让它工作。

Maybe I'm missing something.也许我错过了一些东西。

I tried to set the scrolltop of the element.我试图设置元素的滚动顶部。 Without a result.没有结果。

hourElement.scrollTop(70);

Without a minimal reproducible example I can't dive too much into your code (as your jsfiddle is quite large) but I've created a small jsfiddle which contains a ul like yours with an li for every hour of the day and a button which, when clicked, jumps to the li which represents the current hour of the day.如果没有一个最小的可重现示例,我无法深入研究您的代码(因为您的 jsfiddle 很大),但我创建了一个小型 jsfiddle,其中包含一个像您一样的 ul,每天每个小时都有一个 li 和一个按钮,单击时,跳转到代表当天当前小时的 li。

For example the html looks like:例如 html 看起来像:

<button>
click me
</button>

<ul id="minutes">
    <li class="timeItem">1</li>
    <li class="timeItem">2</li>
    <li class="timeItem">3</li>
    <li class="timeItem">4</li>
    <li class="timeItem">5</li>
    <li class="timeItem">6</li>
    <li class="timeItem">7</li>
    <li class="timeItem">8</li>
    <li class="timeItem">9</li>
    <li class="timeItem">10</li>
    <li class="timeItem">11</li>
    <li class="timeItem">12</li>
    <li class="timeItem">13</li>
    <li class="timeItem">14</li>
    <li class="timeItem">15</li>
    <li class="timeItem">16</li>
    <li class="timeItem">17</li>
    <li class="timeItem">18</li>
    <li class="timeItem">19</li>
    <li class="timeItem">20</li>
    <li class="timeItem">21</li>
    <li class="timeItem">22</li>
    <li class="timeItem">23</li>
</ul>

and in the javascript I do:在 javascript 我做:

$(document).ready(function()
{
    $('button').click(function()
    {
        var hour = new Date().getHours();
        // the hour of the day * the height of one li
        // less 30 to account for the li starting on hour 1
        var toScroll = (hour * 30) - 30;
        $('ul').scrollTop(toScroll);
    });
});

It seems to work as you desire.它似乎可以按您的意愿工作。 If you provide a minimal reproducible example I can look into your code more specifically, in the meantime you can find the fiddle here.如果您提供一个最小的可重现示例,我可以更具体地查看您的代码,同时您可以在此处找到小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM