制作<a> link activate expand text button

时间:2018-12-10 23:32:35

标签: javascript jquery html

I have a button that looks like this:

<button class="button-global button-a" id="more" target="1">More Info</button>

It triggers expanded text with this:

$('.p2').hide();
$('#more').click(function (ev) {
  var t = ev.target
  $('#expanded' + $(this).attr('target')).toggle(50, function(){
    console.log(ev.target)
    $(t).html($(this).is(':visible')? 'Simplify' : 'More Info')
  });
  return false;
});

I want this link (only the aboutus link):

<a class="pcnav navlinks" href="#aboutus-target">About Us</a>

to also trigger the expanded text when clicked but not if expanded is already visible, I don't want the text to change on aboutus like it does for the button,

(made it more straightforward for new people seeing the post as it's gotten a bit confusing)

Any help would be great!

3 个答案:

答案 0 :(得分:2)

类驱动的jQuery

多个<a><button>解决方案

每个<a><button><section class='content...'>具有:

  • 一个“通用静态类” .x

  • 两个“共享状态类” .on.off

为了用相同的类集控制不同的标签,CSS / jQuery选择器被声明为组合,从而为多个标签建立特定的行为,同时共享一个类,该类允许轻松地以组的形式访问所有标签。有关详细信息,请查看演示2 的CSS部分中的注释。


Fiddle

演示2

详细信息在演示中进行了评论。

$('.more').on('click', function() {

  // Get the #id of the closest article.p
  var page = $(this).closest('.p').attr('id');
  
  // Toggle .on/.off on all .x within article.p 
  $(`#${page} .x`).toggleClass('on off');
});

/* 
Exclude .link.on to ensure click event is only
triggered when .off class is assigned to .link
*/
$('.link').not('.on').on('click', function() {

  /*+ Expand .content located within the same .p
  
  var page = $(this).closest('.p').attr('id'); 
  $(`#${page} .x`).addClass('on').removeClass('off');
  
  //-*/
  //*- Expand .content located at this.href
  
  var jump = $(this).attr('href');
  $(`${jump} .x`).addClass('on').removeClass('off');
  
  //-*/
  
  // Scroll to this.href
  $('html, body').animate({
    scrollTop: $(jump).offset().top
  }, 550);
});
html {
  overflow-y: scroll;
  overflow-x: hidden;
}

html,
body {
  background: #fff;
  padding: 20px;
  font: 400 small-caps 16px/1.45 Arial;
}

main {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
}

article {
  background: none;
  display: table;
  width: 100%;
}

nav,
section,
footer {
  background: #222;
  color: #fc3;
  padding: 20px;
  text-align: center;
  font-size: 1rem;
  margin: 0 auto;
  width: 300px;
}

nav {
  padding: 10px 20px;
  margin: 0em auto -0.5em;
  border-top-right-radius: 8px;
  border-top-left-radius: 8px;
}

section p {
  text-align: left;
}

footer {
  padding: 15px 20px 10px;
  margin: -2.8em auto 2.25em;
  border-bottom-right-radius: 8px;
  border-bottom-left-radius: 8px;
}

.link,
footer b {
  font-size: 1.5rem;
  color: #fc3;
}

nav b {
  display: inline-block;
  font-size: 1.5rem;
  width: 1em;
}

button {
  background: #fc3;
  color: #000;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font: inherit;
  font-size: 1.2rem;
  cursor: pointer;
  width: 150px;
}

/*
The next four rulesets demonstrate how only two
classes can be shared between multiple tags and still
provide different behavior and purpose.
*/

.content.off {
  max-height: 0;
  font-size: 0;
  color: transparent;
  opacity: 0;
  transition: color 0.65s, max-height 0.95s, font-size 0.95s, opacity 2.5s;
}

.content.on {
  height: auto;
  max-height: 5000px;
  font-size: 1.1rem;
  color: #fc3;
  opacity: 1;
  transition: all 0.75s;
}

.more.off::before {
  content: 'More Info';
}

.more.on::before {
  content: 'Simplify';
}
<main id='doc'>

  <article id='p1' class='p'>

    <nav>
      <a class="link x off" href="#p2">Next</a>
    </nav>

    <section>
      <button class="more x off"></button>
    </section>

    <section class="content x off">
      <p>A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart.</p>

      <p>I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine.</p>

      <p>I am so happy, my dear friend, so absorbed in the exquisite...</p>
    </section>

    <footer>
      <b>1</b>
    </footer>

  </article>

  <article id='p2' class='p'>

    <nav>
      <a class="link x off" href="#p1">Prev</a>
      <b>|</b>
      <a class="link x off" href="#p3">Next</a>
    </nav>

    <section>
      <button class="more x off"></button>
    </section>

    <section class="content x off">
      <p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.</p>

      <p>He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections.</p>

      <p>The bedding was hardly...</p>
    </section>
    <footer>
      <b>2</b>
    </footer>
  </article>

  <article id='p3' class='p'>
    <nav>
      <a class="link x off" href="#p2">Prev</a>
    </nav>

    <section>
      <button class="more x off"></button>
    </section>

    <section class="content x off">
      <p>The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog.</p>

      <p>Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs.</p>

      <p>Waltz, bad nymph, for quick jigs vex! Fox nymphs grab quick-jived waltz. Brick quiz whangs jumpy veldt fox. Bright vixens jump; dozy fowl quack.</p>
    </section>
    <footer>
      <b>3</b>
    </footer>
  </article>
</main>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


单个<a><button>解决方案

三元条件

以下演示将click事件分别分配给按钮和链接。三元条件的结构如下:

variable = condition ? TRUE : FALSE;

。另外,使用.class.more)代替#id#more)作为更好的选择,但这不是必需的。


演示1

$('.content').hide();

$('.more').on('click', function() {
  var txt = $(this).text() === 'Simplify' ? 'More Info' : 'Simplify';
  $(this).text(txt);
  $('.content').toggle();
});

$('.navLink').on('click', function() {
  $('.content').show();
  $('.more').text('Simplify');
});
<button class="more">More Info</button><br>

<a class="navLink" href="#about">About Us</a>

<p class='content'>
  Content.
</p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


样式为Ryan Wheale'sFiddle分支。

答案 1 :(得分:1)

您将要为click按钮触发#more事件。像这样:

$('a[href="#aboutus-target"]').click((ev) => {
  ev.preventDefault();
  $('#more').trigger('click');
});

如果您只希望导航项扩展文本而从不折叠文本,则需要执行以下操作:

$('a[href="#aboutus-target"]').click((ev) => {
  ev.preventDefault();
  const $more = $('#more');
  const $targ = $('#expanded' + $more.attr('target'));

  // only if the target is not visible
  if( !$targ.is(':visible') ) {
    $more.trigger('click');
  }
});

这是一个小提琴,表明它可以正常工作:

https://jsfiddle.net/tvbhfom8/

答案 2 :(得分:0)

我知道现在有点晚了,但我认为这可能会增加一些东西。

不用javascript的解决方案:使用

标签:

<details>
  <summary>Epcot Center</summary>
  <p>Epcot is a theme park at Walt Disney World Resort featuring...</p>
</details>