【问题标题】:Navigate to a div on a different page with smooth scrolling通过平滑滚动导航到不同页面上的 div
【发布时间】:2017-01-31 21:09:01
【问题描述】:

我正在尝试在单击a 类中的a 元素时进行平滑滚动。它正在重定向到对应的 id。但是平滑滚动不起作用。

index.php

<div class="panel">
    <p><a href="gallary.php#singleclr_led">SINGLE COLOUR LED</a></p>
    <p><a href="gallary.php#doubleclr_led">TWO COLOUR LED</a></p>
    <p><a href="gallary.php#multiclr_led">MULTI COLOUR LED</a></p>
    <p><a href="gallary.php#indoor_led">INDOOR LED DISPLAY</a></p>
    <p><a href="gallary.php#outdoor_led">OUTDOOR LED DISPLAY</a></p>
</div>

jquery

$('.panel a').click(function(){
    $('html, body').animate({
        scrollTop: $( $(this).attr('href') ).offset().top
    }, 500);
    return false;
});

galary.php

 <div class="col-md-6 col-xs-12 gallary-img1" id="multiclr_led">
                <div class="hovereffect">
                    <img src="images/colorimg.jpg" class="img-responsive "alt="">
                    <div class="overlay">
                        <h2>MULTI COLOUR LED</h2>
                        <div class="hover-content">
                            <P>6 colors are possible like in addition to RGB, they are cyan, magenta & yellow. This property is well utilized by the motherboard manufacturer, to show the power indication, RED means lower power than the recommended rating, green means ambient power etc. So they compacted the space requirements.</P>
                        </div>
                    </div>
                </div>
            </div>

找不到问题。帮助这个问题。提前致谢。

【问题讨论】:

  • 所以你在另一个页面(我们命名为index.php),你想打开一个新页面(gallery.php),平滑滚动到对应的id?
  • 是的,你是对的@andreivictor
  • 您能否告诉我们目标 HTML 的外观。是不是类似于&lt;div id="singleclr_led"&gt;... 并且是仅在index.phpgallery.php 中的面板?
  • 查看更新的问题@caramba

标签: jquery css


【解决方案1】:

试试下面的代码(放入gallery.php文件):

$(document).ready(function(){

    var anchor = window.location.hash;

    if ( $(anchor).length > 0 ) {

        $('html, body').animate({
            scrollTop: $(anchor).offset().top
        }, 500);

    }
});

【讨论】:

  • 如果我有通用 jquery 文件我必须做什么
  • 好吧,即使将代码放在一个普通的 jquery 文件中,它也可以工作。但它会平滑滚动到所有锚点,即使它们不在gallery.php 文件中。
  • 你的项目有链接吗?
猜你喜欢
  • 2015-08-11
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多