jquery ui:ajax 页面 +滑块内容(第 2 部分)

发布于 2024-11-03 00:56:51 字数 2536 浏览 0 评论 0原文

跟进我之前发布的帖子。我已经找到了所有要点的答案。但我仍然有一个小问题,我无法正确处理 -

这是我需要将其传递到滑块中的百分比值 -

var percentage = Math.round(offset.left / (scrollContent.width() - scrollPane.width()) * 100);
scrollbar.slider("value",percentage);

每次当我将滑块手柄向右移动时,百分比应该增加。当我加载前两个 ajax 页面时(百分比增加),这很好,但在前两个页面之后,百分比开始下降,直到负值。

我怎样才能找到正确的百分比?

您可以在此处尝试一下,看看我做了什么上面的意思。

下面是整个 jquery 代码:

$(document).ready(function(){

    loadSlide ();

});


function loadSlide ()
{
    //scrollpane parts
    scrollPane = $('#scroll-pane'); // scroll-pane
    scrollContent = $('#scroll-content'); // scroll-content

    //alert(scrollContent.width());

  scrollbar = $("#content-slider").slider({
    min: 0,
    create: loadContent,
    animate: true,
    change: handleSliderChange,
    slide: handleSliderSlide
  });
}

function handleSliderChange(event, ui)
{

  var maxScroll = scrollContent.width() - scrollPane.width();
  scrollPane.animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);

}

function handleSliderSlide(event, ui)
{

  var maxScroll = scrollContent.width() - scrollPane.width();
  scrollPane.attr({scrollLeft: ui.value * (maxScroll / 100) });

}

function loadContent(event, ui) 
{
    $('.load-content').click(function(){

        var path = $(this).attr('href');
        var parent = $(this).parents('.content-item');

        parent.after('<div class="wrapper"></div>');

        var target = $('.wrapper').css({float:'left'});
        target.load(path, function(){

            var width_loaded_content = target.width();
            var offset = target.offset();
            var position = target.position();
            scrollContent.css({width: (scrollContent.width() + width_loaded_content) + 'px'});

            var percentage = Math.round(offset.left / (scrollContent.width() - scrollPane.width()) * 100);
            alert(percentage);
            scrollbar.slider("value",percentage);

            exitContent (target);
        });

        return false;
    });
}

function exitContent (target)
{
    $('.exit-content').click(function(){
        scrollbar.slider("value",0);
        scrollContent.css({width: (1500) + 'px'});
        target.remove();
        return false;
    });
}

另一个问题是,为什么它有时会在 ajax 页面加载后发出两次警报,但有时却不会?它应该总是提醒一次。

有什么想法吗?

Following up on this post I made earlier. I have found out the answers for all the points. But I still have a small tweek that I cannot get it right -

That is the percentage value that I need to pass it into the slider -

var percentage = Math.round(offset.left / (scrollContent.width() - scrollPane.width()) * 100);
scrollbar.slider("value",percentage);

The percentage should increase everytime when I move the slider handle to the right. It is fine when I load the first two ajax pages (the percentage increases), but after these first two, the percentage starts dropping down until negative values.

How can I find the correct percentage?

You can try it out here and see what I mean above.

Below is the entire jquery code:

$(document).ready(function(){

    loadSlide ();

});


function loadSlide ()
{
    //scrollpane parts
    scrollPane = $('#scroll-pane'); // scroll-pane
    scrollContent = $('#scroll-content'); // scroll-content

    //alert(scrollContent.width());

  scrollbar = $("#content-slider").slider({
    min: 0,
    create: loadContent,
    animate: true,
    change: handleSliderChange,
    slide: handleSliderSlide
  });
}

function handleSliderChange(event, ui)
{

  var maxScroll = scrollContent.width() - scrollPane.width();
  scrollPane.animate({scrollLeft: ui.value * (maxScroll / 100) }, 1000);

}

function handleSliderSlide(event, ui)
{

  var maxScroll = scrollContent.width() - scrollPane.width();
  scrollPane.attr({scrollLeft: ui.value * (maxScroll / 100) });

}

function loadContent(event, ui) 
{
    $('.load-content').click(function(){

        var path = $(this).attr('href');
        var parent = $(this).parents('.content-item');

        parent.after('<div class="wrapper"></div>');

        var target = $('.wrapper').css({float:'left'});
        target.load(path, function(){

            var width_loaded_content = target.width();
            var offset = target.offset();
            var position = target.position();
            scrollContent.css({width: (scrollContent.width() + width_loaded_content) + 'px'});

            var percentage = Math.round(offset.left / (scrollContent.width() - scrollPane.width()) * 100);
            alert(percentage);
            scrollbar.slider("value",percentage);

            exitContent (target);
        });

        return false;
    });
}

function exitContent (target)
{
    $('.exit-content').click(function(){
        scrollbar.slider("value",0);
        scrollContent.css({width: (1500) + 'px'});
        target.remove();
        return false;
    });
}

Another issue is, whey it sometimes alerts twice after the ajax pages is loaded but sometimes it does not? it should always alert once.

Any idea?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文