JQuery if 语句条件

发布于 2025-01-08 11:45:00 字数 1804 浏览 0 评论 0原文

我有一个通过操纵左侧值来水平滑动 div 的函数。一切正常,我可以让它左右滚动就好,我遇到的问题是我相信的 if 语句并阻止它滚动。我正在测量 div 的左值,并且我知道它应该根据 div 和屏幕测量值移动的下限和上限。但即使左侧值远大于任一方向的限制,它也不会停止滚动。另外,第一个 if 语句测试面板是否隐藏并且需要滚动,这就是 panelWidth 和 portWidth 变量的用途。不确定我的变量是否没有更新或者什么,有什么建议吗?提前致谢。

JQuery 代码

function portfolioSlider() {

var portWidth = $(window).width()-440;
var panelsWidth = panels * 221;
var portDifference = panelsWidth - portWidth;

if (panelsWidth < portWidth) {
}

else if(panelsWidth > portWidth) {

    var leftPosition = $('#web').css('left').replace(/[^-\d\.]/g, '');
    var upperLimit = 220;
    var lowerLimit = 220 - portDifference;
    var hoverInterval;

    if(leftPosition >= lowerLimit, leftPosition <= upperLimit) {

        $(".arrow_right").css('display','block').css('opacity','1')
        $(".arrow_left").css('display','block').css('opacity','1')          
        $(".arrow_right").hover(
            function() {
                hoverInterval = setInterval(
                function() {
                    $('#web').css('left','-=2');
                    leftPosition = $('#web').css('left').replace(/[^-\d\.]/g, '');
                },10);
            },
            function() {
                clearInterval(hoverInterval);
            }
        );
        $(".arrow_left").hover(
            function() {
                hoverInterval = setInterval(
                function() {
                    $('#web').css('left','+=2');
                    leftPosition = $('#web').css('left').replace(/[^-\d\.]/g, '');
                },10);
            },
            function() {
                clearInterval(hoverInterval);
            }
        );          
    }       
    else {
    }       
};
};

portfolioSlider()

HTML 确实无关紧要,它只是一个带有图像面板的 div,效果很好。再次感谢您的任何建议。

I've got a function that slides a div horizontally by manipulating the left value. Everything works well I can get it to scroll right and left just fine, what I'm having problem with is my if statements i believe and stopping it from scrolling. I'm measuring the left value of the div and I know the lower and upper limit that it should moved based upon div and screen measurements. But it wont stop scrolling even when the left value is far greater than the limits in either direction. Also the first if statement tests if the panels are even hidden and need to be scrolled, thats what the panelsWidth and portWidth variables are for. Not sure if my variables aren't getting updated or what, any suggestions? Thanks ahead of time.

JQuery Code

function portfolioSlider() {

var portWidth = $(window).width()-440;
var panelsWidth = panels * 221;
var portDifference = panelsWidth - portWidth;

if (panelsWidth < portWidth) {
}

else if(panelsWidth > portWidth) {

    var leftPosition = $('#web').css('left').replace(/[^-\d\.]/g, '');
    var upperLimit = 220;
    var lowerLimit = 220 - portDifference;
    var hoverInterval;

    if(leftPosition >= lowerLimit, leftPosition <= upperLimit) {

        $(".arrow_right").css('display','block').css('opacity','1')
        $(".arrow_left").css('display','block').css('opacity','1')          
        $(".arrow_right").hover(
            function() {
                hoverInterval = setInterval(
                function() {
                    $('#web').css('left','-=2');
                    leftPosition = $('#web').css('left').replace(/[^-\d\.]/g, '');
                },10);
            },
            function() {
                clearInterval(hoverInterval);
            }
        );
        $(".arrow_left").hover(
            function() {
                hoverInterval = setInterval(
                function() {
                    $('#web').css('left','+=2');
                    leftPosition = $('#web').css('left').replace(/[^-\d\.]/g, '');
                },10);
            },
            function() {
                clearInterval(hoverInterval);
            }
        );          
    }       
    else {
    }       
};
};

portfolioSlider()

the HTML is really irrelevant its just a div with image panels in it, that works fine. Again thanks for any suggestions.

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

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

发布评论

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

评论(2

池木 2025-01-15 11:45:00

为什么不使用 jQuery.animate()?你可以轻松地做类似的事情

$(".arrow_right").hover(
    function() { this.animate("left", animateTo); },
    function() { this.animate("left", animateFrom); }
);

Why dont you use jQuery.animate()? You can just as easily do something like

$(".arrow_right").hover(
    function() { this.animate("left", animateTo); },
    function() { this.animate("left", animateFrom); }
);
童话里做英雄 2025-01-15 11:45:00

 if(leftPosition >= lowerLimit, leftPosition <= upperLimit) {

不是有效的 JavaScript。

您的意思是布尔值AND吗?

 if(leftPosition >= lowerLimit && leftPosition <= upperLimit) {

即数学符号:lowerLimit <= leftPosition <= upperLimit

This

 if(leftPosition >= lowerLimit, leftPosition <= upperLimit) {

is not valid Javascript.

Did you mean boolean AND?

 if(leftPosition >= lowerLimit && leftPosition <= upperLimit) {

i.e. in mathematical notation: lowerLimit <= leftPosition <= upperLimit

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