Should I experiment with smooth scrolling through an embedded link within an SVG, or perhaps adjusting the scale of the text?

In the process of developing a website, I am using an SVG as the header/navigation. Initially, I embedded the hyperlink text within the SVG like this:

    <a xlink:href="">
        <text transform="matrix(1 0 0 1 495 160)"  
        font-family="'Ubuntu'" font-size="22">Contact</text>
    </a>

However, upon implementing a jQuery scroll feature, I discovered that it does not work with xlinks.

To resolve this issue, I decided to position the text absolutely to overlay the SVG by adding the following style:

#menu {
    position:absolute;
    top:25%;
    left:60%;
    width:100%;
}

I organized the items in a list format:

 <ul id="menu">
        <li><a id="welcome" href="#tex" title="Welcome">Welcome</a></li>
        <li><a id="mission" href="" title="Mission">Mission</a></li>
        <li><a id="hyperlinks" href="" title="Hyperlinks">Hyperlinks</a></li>
    </ul>

Although I managed to make it work, it is limited to a window width of 1980px because the entire SVG rescales while the text remains static.

While I prefer how the "contact" button adapts to scaling along with the SVG, the smooth scroll effect does not apply to it as shown in this example.

There are two potential solutions for this dilemma: either go back to embedding the text inside the SVG and find a way to enable the scroll effect, or explore methods to ensure the text size and position scale precisely with the underlying SVG (SVG on top of SVG?).

Which approach would you recommend for me to address this issue, and what specific aspects should I focus on?

For the complete code, please refer to this link: http://jsfiddle.net/78eh5u52/9/

Answer №1

If you want your links to have a smooth-scroll effect, consider sticking with SVG. Instead of relying on jQuery smooth-scroll, you can create your own custom function for scrolling and easily call it from your SVG.

A sample jQuery function for smooth scrolling could be like this:

function customScrollTo(target){
  $('html,body').animate({
    scrollTop: $(target).offset().top
  }, 1000);
  return false;
}

You can then invoke this function from the SVG as follows:

<a onclick="customScrollTo('#someIdInYourCode')" class="link">
  <text transform="matrix(1 0 0 1 495 160)"  
  font-family="'Ubuntu'" font-size="22">Contact</text>
</a>

Make sure to include "top." in the code so that it can locate your function. Additionally, if you want to change the cursor style, add the following CSS:

.link {
  cursor: pointer;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

.ajax() triggers a page refresh upon pressing the ENTER key

Utilizing Ajax to update the database with a new folder leads to page refresh when hitting ENTER. On the form, I have onkeypress="if(event.keyCode==13) savefolder();". Below is the Javascript code where pressing enter calls savefolder function that sen ...

"Encountering a problem with jQuery AJAX - receiving an error message despite the status code being

Attempting to make a jQuery Ajax request using the following code: $.ajax({ url: '<MY_URL>', type: 'POST', contentType: 'application/json;charset=UTF-8', data: JSON.stringify(data), dataType: 'a ...

Issue with JSON/Java not refreshing innerHTML when onClick event occurs

I'm having an issue with my JavaScript code: <script type="text/javascript"> $(document).ready(function() { $('#domaincheckbutton').click(function() { var finalMessage = document.getElementById('finalMessage'); ...

What is the best way to expand the header, content, and footer colors to cover the entire width of a page while keeping everything centered within a fixed width

I managed to get the header and main content area to stretch across the full page, while keeping the content centered. But now I am facing an issue with the footer not stretching like the header. How can I make the footer stretch as well? HTML: <body ...

JavaScript - Delayed Text Appearance with Smooth Start

I want to create a landing page where the text appears with a slight delay - first, the first line should be displayed followed by the second line. Both lines should have an easing effect as they appear. Below is a screenshot of the section: https://i.sst ...

What is the best way to prevent <hr> from covering a <div> in CSS?

I'm currently facing a challenge in moving an <hr> element behind my div element. I have already tried positioning, but it didn't work as expected. The <hr> is still visible through the white parts, most likely because they are transp ...

Using onClick from another tag, React can dynamically change the style of a className

I am looking to dynamically change the position of a div with the class name "myMenu" using an onClick event triggered from an h1 tag element. Specifically, my current setup looks like this: <div className="myMenu"> <button onClick={()=> t ...

Using the textbox input to trigger an alert message

Just starting out with JavaScript and I'm attempting to create an alert box that not only displays the message "Thank You For Visiting" when you click the Enter button, but also includes the name entered into the text box. While I have successfully im ...

Having trouble with JQuery's $get or $getJSON functions?

I attempted to use the following code in order to send an HTTP GET request to retrieve data from a server. It seems like the request is not making it to the server at all. Is there something I might be overlooking? Even when trying $.get(url, function(dat ...

Aligning a number in the center both vertically and horizontally within a div that is inside a table-cell

Is there a way to center a number both vertically and horizontally within a div that needs to be 60px in width and height, shaped like a circle, and placed in the center of a table-cell? I've managed to center the div within the table-cell but can&apo ...

I am looking to retrieve the name of the individual currently logged in

I'm looking for help to integrate my PHP code that displays the current logged-in user's username into a jQuery script, so that the username can be automatically filled. I'm new to jQuery and not sure how to achieve this on my own. Here is ...

Customizing error messages to display validation errors instead of default text errors in the Django UserCreationForm

I'm encountering an issue with the registration form on my website. The form itself is straightforward, but I'm facing a problem where if a username is already taken or if the two password fields don't match, Django doesn't respond afte ...

Ways to merge the concepts of "if" and "if not

I've written a function that needs to run only if a certain condition is not true: $(window).width() > 990) !$('body').hasClass("home") I attempted combining them like this: if( $(window).width() > 990) && !$('body&apo ...

Implementing dynamic page loading with ajax on your Wordpress website

I'm currently facing an issue with loading pages in WordPress using ajax. I am trying to implement animated page transitions by putting the page content into a div that I will animate into view. However, my current logic only works correctly about 50% ...

Utilize the power of AJAX for efficiently sorting search results retrieved from MySQL

I'm in the process of designing a flight search page. The initial page contains a form, and when the submit button is clicked, the search results are displayed on the second page. Here's the link to the first page: To test it out, please follow ...

Change the color of child li to white when hovering over the parent li, and vice versa

Let me start off by explaining the issue I'm facing. Take a look at to get a better understanding of what I'm referring to. It needs to be a specific page, not the home page, so you can see the problem for yourself. Now, when you hover over the ...

Angular 2: Dealing with Missing Image Loading

When viewing an HTML file containing the following code: <div> <img src="New-Google-Logo.png"/> </div> The image file New-Google-Logo.png is expected to load from the same folder as the HTML file. However, after running ng s ...

Gulp does not generate any files

Hey there, I'm brand new to using node.js and coding in general. I recently attempted to convert SCSS to CSS using gulp. My gulpfile.js seems to be correct, but when I try running "gulp styles" in the node.js command prompt, I receive the following ou ...

What steps are involved in setting up a dropdown side navbar?

Is there a way to automatically activate the next dropdown in the side navbar when I click on the 'Next' button? And similarly, can we open the previous dropdown in the side navbar when clicking the 'Previous' button? ...

Sliding Gallery Automation

Seeking a sliding gallery that automatically transitions slides upon page load. Unsure how to implement this feature. Demo: Currently using a similar gallery but looking for an automatic scrolling option upon loading. In search of a new script with the ...