简体   繁体   English

jQuery UI Slider不起作用(提供了提琴)

[英]jQuery UI Slider doesn't work (fiddle is provided)

I've inserted a jQuery UI slider in my website, which works pretty good. 我在网站上插入了一个jQuery UI滑块,效果很好。 Here you can see the slider: FIDDLE 在这里您可以看到滑块: FIDDLE

When you click on 'back to the top' you see it just scrolls to the top. 当您单击“返回顶部”时,您会看到它只是滚动到顶部。 But when I go with the slider to for example 1918, it just goes without any slide. 但是当我将滑块移到例如1918时,它就没有任何滑动。

Is there any way to apply this scroll jquery to the jQuery slider, so that also scrolls down the page just like the button 'back to the top'. 有什么方法可以将此滚动jquery应用于jQuery滑块,从而也可以像“返回顶部”按钮一样向下滚动页面。

Thanks in advance. 提前致谢。

Here's the code for the smooth scroll: 这是平滑滚动的代码:

$(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();    
        var target = this.hash,
        $target = $(target);    
        $('html, body').stop().animate({
            'scrollTop': $target.offset().top
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});

Use the same approach for scrolling down that you are using for scrolling up. 使用与向上滚动相同的方法向下滚动。 The change function for your slider should use the same animate method. 滑块的更改功能应使用相同的动画方法。 You can also simplify your function by removing the if statement and reusing the array for obtaining the anchor: 您还可以通过删除if语句并重新使用该数组以获得锚来简化函数:

change: function(event, ui) { 
    $('html, body').animate({ scrollTop: $('#'+araObj[ui.value-1]).offset().top }, 900); }
 });    

Changing just that function would give you this: 仅更改该功能将为您提供:

$(function() {
    var araObj = new Array( 1900, 1918, 1931, 1975, 1976, 1978, 2000, 2002, 2004, 2012, 2013 );

    $("#slider-range-max").slider({
            min: 0,
            max: araObj.length,
            value: 0,
            create: function() {

                for (i=0;i<=araObj.length;i++)
                {
                    $(this).append($('<span></span>').css("left",((i+0.97)*(100/araObj.length))+"%").addClass("slider-digit").text(araObj[i]));
                };
            },
            change: function(event, ui) { 
                  $('html, body').animate({ scrollTop: $('#'+araObj[ui.value-1]).offset().top }, 900); }
        });    
    });

// This is for the smooth scroll

$(document).ready(function(){
    $('a[href^="#"]').on('click',function (e) {
        e.preventDefault();

        var target = this.hash,
        $target = $(target);

        $('html, body').stop().animate({
            'scrollTop': $target.offset().top
        }, 900, 'swing', function () {
            window.location.hash = target;
        });
    });
});

One major problem is that you are initializing $(function() { and $(document).ready(function(){ all the code can and should be written in one initialization function that runs when the document is ready. 一个主要问题是,您正在初始化$(function() {$(document).ready(function(){所有代码都可以并且应该被写入一个在文档准备就绪时运行的初始化函数中。

The way you have it setup currently is causing some issues with the document and sometimes your elements are not being found by Id because of this. 目前,您使用它进行设置的方式导致该文档出现一些问题,因此,有时Id找不到您的元素。

I would consolidate your code into one ready function: 我会将您的代码合并为one就绪函数:

Something like this would work: 这样的事情会起作用:

$(document).ready(function(){
    var araObj = new Array( 1900, 1918, 1931, 1975, 1976, 1978, 2000, 2002, 2004, 2012, 2013 );

    $("#slider-range-max").slider({
            min: 0,
            max: araObj.length,
            value: 0,
            create: function() {

                for (i=0;i<=araObj.length;i++)
                {
                    $(this).append($('<span></span>').css("left",((i+0.97)*(100/araObj.length))+"%").addClass("slider-digit").text(araObj[i]));
                };
            },
        change: function(event, ui) { 
             var year = '';
             var val=ui.value;
             if(val == 1)
             {
                 year = '1900';    //same tab
             }
             else if(val == 2)
             {
                 year = '1918';
             }
             else if(val == 3)
             {
                 year = '1931';
             }
             else if(val == 4)
             {
             window.location.href='http://www.google.com';
             }
             else if(val == 5)
             {
             window.location.href='http://www.google.com';
             }
            if(year != ''){
                $('html, body').stop().animate({
                    'scrollTop': $('#'+year).offset().top
                }, 900, 'swing', function () {
                });     
            }
          } 
        });
    console.log(araObj);

    $('.link').click(function(e){
        e.preventDefault();
        $('html, body').stop().animate({
            'scrollTop': 0
        }, 900, 'swing', function () {

        });        
    });

});

Example: 例:

http://jsfiddle.net/trevordowdle/wqB5q/9/ http://jsfiddle.net/trevordowdle/wqB5q/9/

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

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