同一页面ID中某个元素的锚定标记不起作用 [英] Anchor tag to some element in same page id doesn't work

查看:23
本文介绍了同一页面ID中某个元素的锚定标记不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哈哈。我的小项目遇到了麻烦。我已经创建了<a href="slogannya">some icon</a><blockquote id="slogannya">,但当我单击<a>标记时,它不起作用。以下是我的代码:

HTML

<section class="to-content">
        <a class="scrollTo" href="#slogannya">
          <button type="button">
            <i class="material-icons">keyboard_arrow_down</i>
          </button>
        </a>
      </section>
      <section class="slogan">
        <blockquote id="slogannya">
          <p>Wanna create an event? <br> or you want to give some advice to us? Use the form below </p>
        </blockquote>
      </section>

jQuery

$(document).ready(function () {
  $(".scrollTo").on('click', function (e) {
      e.preventDefault();
      var target = $(this).attr('href');
      $('html, body').animate({
          scrollTop: ($(target).offset().top)
      }, 2000);
  });

});

我如何才能做到这一点?请帮帮我:)

编辑

我知道我的问题在哪里。在我的项目中,我有一个名为‘General al.css’的css(此css内容css重置和任何页面的其他样式)。在这个css中,它使任何元素都是box-sizing: border-box;和其他样式。我不知道具体是什么问题。但当我去掉我的"General.css"时,平滑的卷轴就起作用了。感谢你的回答,朋友们:),我很感激!!

HTMLnchor

您所需要的只是推荐答案一些nchor和任何您想要的元素(甚至更多nchor疯狂)。Btw目标id不起作用的原因是href需要#的URL前缀。前男友。<a href="#ID"...它可能不起作用的另一个原因是<a>nchor和Destination元素彼此太近,因此理想情况下,目标至少应该足够远,需要滚动才能到达。

  • 对于目标元素,确保它有一个#id。例如<div id='ID'...

  • 接下来,为每个目的地创建一个或多个<a>nchor,并将其href值设置为#+ID.例如。<a href='#ID...

  • 如果目标元素恰好是<a>Nchor,我们也可以使用name属性。前男友。<a name='NAME'...

演示

数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
main {
  margin: 50px auto;
}

a[name] {
  background: black;
  color: white
}
<main id='main'>
  <header>
    <h1>Anchor Test</h1>
    <nav>
      <a href='#s0'>Section 1</a>
      <a href='#s1'>Section 2</a>
      <a href='#s2'>Section 3</a>
    </nav>
    <nav id='toTop'>
      <a href='#end'>End 🔻</a>
      <a href='#a0'>Two-handed Weapon</a>
      <a href='#a1'>Cheapskates</a>
      <a href='#a2'>Devil Dance</a>
    </nav>

  </header>
  <hr>
  <section id='s0'>
    <h2>Section 1</h2>

    <p>Base save bonus checked cold dangers cold domain compulsion subschool constrict current hit points dead diminutive domain spell flank gargantuan hit hit die improved grab knocked down manufactured weapons movement modes negative energy plane off hand
      psionics result skill points spell version threat total cover transitive plane turn resistance turn undead undeath domain. Angel subtype caster level concealment continuous damage death domain energy drained extraplanar subtype guardinal subtype
      improved evasion lethal damage medium melee attack pounce rake subject <a name='a0'>two-handed weapon</a> undead type.</p>
  </section>
  <hr>
  <section id='s1'>
    <h2>Section 2</h2>
    <p>And it better be long as the troubles in my head hammer my bones on the anvil of daylight i'd buy some time i'm so tired don't know where to begin into your brother's cup just to see the dogs runnin on a blackboard night ringin' in my ears the information
      is ravenous you're acting like it's chill, when the deal's getting. And all the toilets are overflowing and i'm falling out of the conversation and the band playin' down below are those dogs or are those dogmas? black can spell to be day in the
      club tonight doldrums are pounding, <a name='a1'>cheapskates</a> are clowning this town flap your wings and leap out the window how do you play? she doesn't even know it's wrong she's the sister of avarice sipping the golden days on a riptide snooty.
      some static is lulling me to sleep talkin' to the devil and drinkin' a coke the dishes wash good to meet you in that land try not to drown where can you duck when they shoot you full of pigeon holes who are you? you said go.</p>
  </section>
  <hr>
  <section id='s2'>
    <h2>Section 3</h2>
    <p>A little knowledge is a dangerous thing. boys will be boys. curiosity killed the cat. cut your coat according to your cloth. do not bite the hand that feeds you. do not change horses in midstream. first impressions are the most lasting. he who hesitates
      is lost. if at first you do not succeed, try, try again. it is best to be on the safe side. kindness in words creates confidence. kindness in thinking creates profoundness. kindness in giving creates love. let the buyer beware. let your hair down.
      lightning never strikes twice in the same place. money makes many things, but also makes <a name='a2'>devil dance</a>. never speak ill of the dead. one might as well be hanged for a sheep as a lamb. penny wise and pound foolish. practice makes perfect.
      silence is golden. talk is cheap. the grass is always greener on the other side of the fence. the road to hell is paved with good intentions. there is no accounting for tastes. third time is a charm. to the victor go the spoils. walnuts and pears
      you plant for your heirs. you cannot make an omelette without breaking eggs..</p>
  </section>
</main>
<footer>
  <nav>
    <a href='#s0'>Section 1</a>
    <a href='#s1'>Section 2</a>
    <a href='#s2'>Section 3</a>
  </nav>
  <nav id='end'>
    <a href='#main'>Top 🔺</a>
  </nav>
</footer>

这篇关于同一页面ID中某个元素的锚定标记不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆