4

I have a button on the top of my page that links to /#tabs (index id="tabs"). it uses a script to scroll nicely down to the id but it changes the url to www.domain.com/#tabs how can I remvoe the #tabs part? I was thinking about doing it in .htaccess but not sure if thats possible and its proerbly a bad idea.

heres the html:

    <a class="smoothscroll" href="/#tabs">
      <div class="scroll-down"></div>
    </a>
  </header>

<br />
  <div id="tabs" style="padding:20px;"></div>

  <div class="tabs">
    <h1>About</h1>
    <div class="p">
      about us
      <a href="/contact"><input type="submit" value="Contact Us" class="btn" name="contact" style="min-width:15%;"/></a>
    </div>  
  </div>

<a class="smoothscroll" href="/#tabs">
      <div class="scroll-down"></div>
    </a>

Is the button part and it uses this script to scroll smoothly

<script>
/*----------------------------------------------------*/
/* Quote Loop
------------------------------------------------------ */

function fade($ele) {
    $ele.fadeIn(1000).delay(3000).fadeOut(1000, function() {
        var $next = $(this).next('.quote');
        fade($next.length > 0 ? $next : $(this).parent().children().first());
   });
}
fade($('.quoteLoop > .quote').first());


/*----------------------------------------------------*/
/* Smooth Scrolling
------------------------------------------------------ */

jQuery(document).ready(function($) {

   $('.smoothscroll').on('click',function (e) {
      e.preventDefault();

      var target = this.hash,
      $target = $(target);

      $('html, body').stop().animate({
          'scrollTop': $target.offset().top
      }, 800, 'swing', function () {
          window.location.hash = target;
      });
  });

});


TweenMax.staggerFrom(".heading", 0.8, {opacity: 0, y: 20, delay: 0.2}, 0.4);
</script>

just forget about the top of the script, thats just a part for the index.

Volcan 3
  • 99
  • 1
  • 2
  • 11

4 Answers4

3

You can do it otherwise like this:

In html

<a href="" class="smoothscroll" onclick="functionforscroll('tabs')">
<div class="scroll-down"></div></a>

in js

var functionforscroll = function(id){
    var reqId = "#"+id;
    window.scrollTo(0, $(reqId).offset().top-85);
}

this way you can scroll to the required position without changing the url

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Vicky Kumar
  • 1,358
  • 1
  • 14
  • 26
0

All you need to do is put a tabindex="-1" to the element you want the focus to, and on the event of focusout/blur remove that tab index. This will enable the element not to be tabbed, but will bring the focus to the element.

I have done that using JQuery:

(ELEMENT_FOR_FOCUS).attr('tabindex', -1).on('blur focusout', function () {
                $(this).removeAttr('tabindex');
            }).focus();
0

Try use "data-target" instead "href". Works for me.

  • 1
    Would have been great to provide some basic JS to go with this, as it is this is just a *hint* to go Google something, or rake the OP's code and match it up – James Nov 22 '21 at 14:35
0

Changing Vicky Kumar's answer, this will work.

HTML:

<button class="smoothscroll" onclick="functionforscroll('id')">
<div class="scroll-down"></div></button>

Javascript

const functionforscroll = function(id){
    const reqId = "#"+id;
    window.scrollTo(0, $(reqId).offset().top-85);
}

You can style the button to be like the rest of your links in your project. One thing is to set outline to none, so that when someone clicks the button it will not show the border/outline of the button.

GreatCoder
  • 61
  • 1
  • 11