【问题标题】:Jquery scroll to element only if not already visibleJquery 仅在不可见时滚动到元素
【发布时间】:2013-10-01 17:25:13
【问题描述】:

我有一个如下所示的 div:

<div id="ProductPriceWrap" class="ProductPriceWrap">
                <div class="DetailRow RetailPrice" style="">
                    <span class="Label">MSRP:</span>
                    <strike>$249.00</strike>
                    <span class="YouSave" style=""> (You save <span class="YouSaveAmount">$174.00</span>)</span>
                </div>

                <div class="DetailRow PriceRow" style="">
                    <div class="Value">
                        <em id="ProductPrice" class="ProductPrice VariationProductPrice" style="color: black; ">$75.00</em>

                    </div>
                </div>


            </div>   

我制作了这个脚本来帮助客户查看所选择的选项何时改变了价格:

$(document).ajaxSuccess(function(){

var currentPrice = $.trim($("#ProductPrice").text());

if(currentPrice == "%%GLOBAL_ProductPrice%%") 
{
        $("#ProductPrice").css('color','black');

        $("#ProductPrice").removeClass("PriceChanged")
}
else
{
    $("#ProductPrice").css('color','red');

    $('html, body').animate({
        scrollTop: $("#ProductPriceWrap").offset().top
    }, 1000);

    $("#ProductPriceWrap").animate({backgroundColor: "#ff0000" });

    $("#ProductPriceWrap").animate({backgroundColor: "#ffffff" });

    $( "#ProductPrice" ).addClass( "PriceChanged" );
    }

});
</script>​

我想更改滚动到#ProductPriceWrap 的函数,以便仅在滚动通过它时才会滚动到该元素。意思是如果它已经可见,则不要滚动到该元素。我对 JS 和 JQ 很陌生,甚至不知道从哪里开始。非常感谢任何输入!谢谢!

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您似乎正在寻找选择器 :visible 和函数 .animate()。

最后你会得到类似的东西:

if ($("#ProductPriceWrap:not(:visible)")) {
  $("html, body").animate({
    scrollTop: $("#ProductPriceWrap").offset().top
  }, 1000);
}

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 2020-08-05
    • 2019-03-22
    • 2011-08-06
    • 2012-07-09
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多