在 jQuery UI Accordion 脚本中添加 ScrollTo

发布于 2024-10-26 12:19:26 字数 3261 浏览 6 评论 0原文

我到处搜索,但没有找到对我的具体情况有帮助的帖子。我是 jQuery 的新手,喜欢它的广泛用途。我的手风琴脚本有问题,我需要添加 ScrollTo,以便当选择某个部分时,如果它位于视图上方,它会向上滚动窗口。我希望这是有道理的。谢谢你的帮助。

<script type="text/javascript">
        /* <![CDATA[ */
        jQuery().ready(function(){
            jQuery('#leftnav-navigation').accordion({
                active: false,
                header: '.head',
                navigation: true,
                collapsible: true,
                animated: 'easeslide',
                autoheight: false,
                alwaysOpen: false,
            });

            var accordions = jQuery('#leftnav-navigation'); 

            jQuery('#switch select').change(function() {
                accordions.accordion("activate", this.selectedIndex-1);
            });
            jQuery('#close').click(function() {
                accordions.accordion("activate", -1);
            });
            jQuery('#switch2').change(function() {
                accordions.accordion("activate", this.value);
            });
            jQuery('#enable').click(function() {
                accordions.accordion("enable");
            });
            jQuery('#disable').click(function() {
                accordions.accordion("disable");
            });
            jQuery('#remove').click(function() {
                accordions.accordion("destroy");
                wizardButtons.unbind("click");
            });
            return false;
        });
        /* ]]> */
    </script>

感谢ckaufman 的帮助。这是最终的工作代码。我希望这可以帮助有需要的人。

<script type="text/javascript">
        /* <![CDATA[ */
        jQuery().ready(function(){
            jQuery('#leftnav-navigation').accordion({
                active: false,
                header: '.head',
                navigation: true,
                collapsible: true,
                animated: 'easeslide',
                autoheight: false,
                alwaysOpen: false,
            });

            var accordions = jQuery('#leftnav-navigation'); 

            jQuery('#switch select').change(function() {
                accordions.accordion("activate", this.selectedIndex-1);
            });
            jQuery('#close').click(function() {
                accordions.accordion("activate", -1);
            });
            jQuery('#switch2').change(function() {
                accordions.accordion("activate", this.value);
            });
            jQuery('#enable').click(function() {
                accordions.accordion("enable");
            });
            jQuery('#disable').click(function() {
                accordions.accordion("disable");
            });
            jQuery('#remove').click(function() {
                accordions.accordion("destroy");
                wizardButtons.unbind("click");
            });

            jQuery('#leftnav-navigation').click(
                function() {
                    var window_top = $(window).scrollTop();
                    var div_top = $(this).offset().top;
                        if (window_top > div_top){
                            $('html, body').animate({scrollTop:div_top}, 300);
                }
            });

            return false;
        });
        /* ]]> */
    </script>

I've search high and low and have not found a post that has helped my specific situation. I am new to jQuery and love its wide range of uses. I have an issue with my accordion script and I need to add the ScrollTo so that when a section if selected it scrolls the window up if it is above the view. I hope this makes sense. Thank you for the help.

<script type="text/javascript">
        /* <![CDATA[ */
        jQuery().ready(function(){
            jQuery('#leftnav-navigation').accordion({
                active: false,
                header: '.head',
                navigation: true,
                collapsible: true,
                animated: 'easeslide',
                autoheight: false,
                alwaysOpen: false,
            });

            var accordions = jQuery('#leftnav-navigation'); 

            jQuery('#switch select').change(function() {
                accordions.accordion("activate", this.selectedIndex-1);
            });
            jQuery('#close').click(function() {
                accordions.accordion("activate", -1);
            });
            jQuery('#switch2').change(function() {
                accordions.accordion("activate", this.value);
            });
            jQuery('#enable').click(function() {
                accordions.accordion("enable");
            });
            jQuery('#disable').click(function() {
                accordions.accordion("disable");
            });
            jQuery('#remove').click(function() {
                accordions.accordion("destroy");
                wizardButtons.unbind("click");
            });
            return false;
        });
        /* ]]> */
    </script>

Thanks to ckaufman for his help. Here is the final working code. I hope this helps someone in need.

<script type="text/javascript">
        /* <![CDATA[ */
        jQuery().ready(function(){
            jQuery('#leftnav-navigation').accordion({
                active: false,
                header: '.head',
                navigation: true,
                collapsible: true,
                animated: 'easeslide',
                autoheight: false,
                alwaysOpen: false,
            });

            var accordions = jQuery('#leftnav-navigation'); 

            jQuery('#switch select').change(function() {
                accordions.accordion("activate", this.selectedIndex-1);
            });
            jQuery('#close').click(function() {
                accordions.accordion("activate", -1);
            });
            jQuery('#switch2').change(function() {
                accordions.accordion("activate", this.value);
            });
            jQuery('#enable').click(function() {
                accordions.accordion("enable");
            });
            jQuery('#disable').click(function() {
                accordions.accordion("disable");
            });
            jQuery('#remove').click(function() {
                accordions.accordion("destroy");
                wizardButtons.unbind("click");
            });

            jQuery('#leftnav-navigation').click(
                function() {
                    var window_top = $(window).scrollTop();
                    var div_top = $(this).offset().top;
                        if (window_top > div_top){
                            $('html, body').animate({scrollTop:div_top}, 300);
                }
            });

            return false;
        });
        /* ]]> */
    </script>

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

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

发布评论

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

评论(2

〆一缕阳光ご 2024-11-02 12:19:26

我认为沿着这些思路的一些事情可能会起作用。我会解释一下,也许通过一些调整你就可以实现它。

jQuery('#divID').click(
  function() {
  var window_top = $(window).scrollTop();
  var div_top = $(this).offset().top;
     if (window_top > div_top){
     $('html, body').animate({scrollTop:div_top}, 300);
     }
    });

单击绑定的事件将检测“div_top”和“window_top”...如果div位于window_top上方,它将滚动到div_top的位置。值得一试,希望有帮助。

I think something along these lines may work. I'll explain and maybe with some tweaking you can implement it.

jQuery('#divID').click(
  function() {
  var window_top = $(window).scrollTop();
  var div_top = $(this).offset().top;
     if (window_top > div_top){
     $('html, body').animate({scrollTop:div_top}, 300);
     }
    });

The click binds the event which will detect the 'div_top' and 'window_top'... If the div is above the window_top it will scroll to the location of div_top. Worth a try, hope it helps.

梦幻的心爱 2024-11-02 12:19:26

实际上,我已经这样做了...

您需要将 jQuery 的scrollTo.js 添加到您的项目中,然后将 ui.accordion.js 文件替换为 JSFiddle 中提供的文件: http://jsfiddle.net/Jaybles/6EWAF/

Actually, I have already done this...

You need to add jQuery's scrollTo.js to your project, and then replace the ui.accordion.js file with the one in the JSFiddle provided: http://jsfiddle.net/Jaybles/6EWAF/

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