Hover without proper anchoring / move to section on click without anchor tag

I need assistance with a jump tab feature I am implementing on my portfolio website. I have encountered a couple of issues that I could use some help with.

https://i.sstatic.net/8hwgL.png

<ul class="section">
   <li><span>home</span></li>
   <li><span>about</span></li>
   <li><span>project</span></li>
   <li><span>contact</span></li>
<ul>
#main .section {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 50%;
    right: 30px;
    text-align: right;
    z-index: 10;
    margin-top: -100px;
}

#main .section li {
    list-style: none;
    background-color: #fff;
    position: relative;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    margin-top: 30px;
    transform: scale(1);
    transition: all ease 0.5s;
}

#main .section li:hover {
    background-color: rgb(78, 78, 78);
    transform: scale(1.5);
}

#main .section li span {
    display: block;
    position: absolute;
    width: 100px;
    height: 20px;
    text-align: right;
    right: 25px;
    color: #fff;
    text-transform: uppercase;
    opacity: 0;
}

#main .section li span:hover {
    opacity: 1;
}

I am facing two main challenges. Firstly, the text is supposed to be displayed only when hovering on span, but it is not showing up when hovering on li which is a circular shape. Can you spot any errors in my code?

Secondly, I am struggling with the functionality of jumping to the sections. I have attempted to use the scrollIntoView method, but I am unsure how to target multiple elements within one function. Below is my current attempt, which is not functioning correctly:

let jumpbtn = document.querySelectorAll(".section li");

function jump(){
    for(var i = 0; i < jumpbtn.length; i++){
        jumpbtn[i].addEventListener("click", function(){
            this.id.scrollIntoView({behavior: "Smooth"});
        })
    }
}

Answer №1

Hover over the circle to view the text as well :)

Give this a shot

.section li:hover span {
    opacity: 1;
}

Rather than

.section li span:hover {
    opacity: 1;
}

Next, I included the class of each li in the onclick() event.

<li onclick="jump(this.className)" class="home">

then, retrieve the target element using this class name and scroll to the target element's id with the following code:

function jump(TarId){
   var target = document.getElementById(TarId);
   document.getElementById(TarId).scrollIntoView({behavior: "smooth"});
}

Live Demo

function jump(TarId){
   var target = document.getElementById(TarId);
   document.getElementById(TarId).scrollIntoView({behavior: "smooth"});
}
.section {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: calc(25% - 100px);
    right: 30px;
    text-align: right;
    z-index: 10;
}

.section li {
    list-style: none;
    background-color: #fff;
    position: relative;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    margin-top: 25px;
    transform: scale(1);
    transition: all ease 0.5s;
}

.section li:hover {
    background-color: rgb(78, 78, 78);
    transform: scale(1.5);
}

.section li span {
    display: block;
    position: absolute;
    width: 100px;
    height: 20px;
    text-align: right;
    right: 25px;
    color: #fff;
    text-transform: uppercase;
    opacity: 0;
}

.section li:hover span {
    opacity: 1;
}
#main{
    background:#111;
    position:relative;
    height:200px;
}
.content{
  width:100%;
  margin-bottom:200px;
}
h1{
  color:#00cc00;
}
<div id="main">
  <ul class="section">
     <li onclick="jump(this.className)" class="home"><span>home</span></li>
     <li onclick="jump(this.className)" class="about"><span>about</span></li>
     <li onclick="jump(this.className)" class="project"><span>project</span></li>
     <li onclick="jump(this.className)" class="contact"><span>contact</span></li>
  <ul>
</div>

<div class="content">
  <div id="home">
    <h1>Home Content</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>
  </div>
  <div id="about">
    <h1>about Content</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>
  </div>

  <div id="project">
    <h1>project Content</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>
  </div>

  <div id="contact">
    <h1>contact Content</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<p>
  </div>
</div>

Answer №2

Check out the code snippet below, it might be useful for you.

function moveToDiv(target){
            document.getElementById(target).scrollIntoView({
                behavior: 'smooth'
            });
        }
.section{
    display:block;
    }
    .section-area{
    height: 300px;
    width: 100%;
    border: 10px solid red;
    background: gray;
    font-size: 30px;
    box-sizing:border-box;
    display:block;
    }
<ul class="section">
       <li onclick="moveToDiv('home')"><span>home</span></li>
       <li onclick="moveToDiv('about')"><span>about</span></li>
       <li onclick="moveToDiv('project')"><span>project</span></li>
       <li onclick="moveToDiv('contact')"><span>contact</span></li>
    </ul>
    <div class="section-area" id="home">Home</div>
    <div class="section-area" id="about">About</div>
    <div class="section-area" id="projects">Projects</div>
    <div class="section-area" id="contact">Contact</div>

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

Learn the best way to send query parameters through the Next.js navigation router and take advantage of

Currently, I am implementing import { useHistory } from 'react-router-dom' const history = useHistory() ... history.push('/foo?bar=10') However, only the 'foo' part is being pushed into the url. What is the correct way to pas ...

Nivo Slider's Interactive Image Mapping

I am encountering difficulties in integrating an image map into my nivo slider. The code I am currently using is shown below, and you can access the site by clicking on this link. Do you have any suggestions on how to resolve this issue? <div class=" ...

How can you retrieve the index of the outer repeater item within nested ng-repeaters in AngularJS?

If I have multiple ng-repeat loops nested within each other like in the following example: <div ng-repeat="outeritem in outerobject"> <div ng-repeat="inneritem in innerobject" ng-click="function(inneritem.key, $index)"></div> <d ...

Concealing the source code within a Next.js application

Currently, I am utilizing next.js for a project. We have a contact page where we display email addresses in cards, but we want to prevent bots from accessing this information. A solution was discovered by one of my colleagues to hide the email addresses i ...

Adjust an UpdatePanel with client-side code triggering server-side operations

I am fairly new to asp.net and have been experimenting with it for around a week now. Currently, I have a page that interacts with a web service, continuously checking its progress (shown in an UpdatePanel) until completion. Once the process is completed, ...

Understand which form has been submitted

I have eight Forms in a page (Form0, Form1, Form2 and so forth). Each form, when submitted, sends data to ReassignPreg.php using JavaScript, which then searches for the data in the database and returns it as JSON. The corresponding divs on the page are the ...

Display each table within a modal that is triggered by a separate table loop

I have a modal that includes a table. The modal is loaded from a looped table, and despite my confident code, the display seems off. When I run the code, this odd result occurs. Click the link to view the image. <tbody> <?php ...

The gap between list items(`<li>`s)

Feeling a bit perplexed here. I'm working on creating a page to showcase images. My goal is to have 5 images displayed per row, with maximum spacing when the window is at its widest (~950 px). However, as the window size decreases, I want the images t ...

Using Jquery to manipulate arrays based on options selected from a dropdown menu

I am currently working on a feature that suggests tags based on the selected category. This involves using a select box to choose a category and then displaying sub-categories in a list. Here is the current setup: <select id="categorySelect"> < ...

Ways to conceal an element in Angular based on the truth of one of two conditions

Is there a way to hide an element in Angular if a specific condition is true? I attempted using *ngIf="productID == category.Lane || productID == category.Val", but it did not work as expected. <label>ProductID</label> <ng-select ...

Obtain the jQuery dialog's closure event within the $.Ajax completion function

I have developed a custom jQuery plugin that utilizes jQuery Dialog to showcase messages. Specifically, I am using it within my jQuery $.ajax -->done function. My goal is to capture the close event of the Dialog in the .ajax function so that I can redir ...

Updating image URL for grouped objects with Fabric JS

Check out this link :http://jsfiddle.net/mishragaurav31/ang58fcL/#base I created two groups of objects; one consisting of a circle and text, and the other with two images. When I try to change attributes for group 1, it works fine. However, when attempt ...

What's the source of this undefined error and is there a way to eliminate it from the code?

After successfully filtering and reducing the data, I encountered an issue with undefined. I am trying to figure out what is causing this and how I can either remove it or make it visible on the screen without being invisible. You can also check the codes ...

Leveraging the same React component in multiple instances and streamlining requests - NextJS vs React

Currently, I'm working on a chat application using NextJS 14. Each time a message is sent or received, there is a small icon next to the chat message that represents the user who sent the message. To achieve this, I have a ChatMessageUI component tha ...

The preventDefault function fails to work in Firefox

My input is called shop_cat_edit and I've included the following code. However, it seems to work fine in IE but doesn't work in FireFox. Can anyone help me figure out what's wrong? $('[name=shop_cat_edit]').on('click',fu ...

Overlapping Bootstrap columns detected when viewing on smaller screens

In an attempt to create a user-friendly layout, I developed two columns: one for the title and another for projects, with the latter being scrollable. The design functions as intended on desktop view but encounters an issue when the viewport is reduced to ...

Text fades into view from the bottom after a line break using CSS and JavaScript

I'm looking to create a unique fade reveal text effect for a multi-line H1. The goal is for each line of the text to fade up from its own row, rather than simply revealing from the bottom. Here's an example I've already developed, but it cu ...

execute a C++ application within a web browser using HTML

Recently, I created a captcha program using c++ and sfml. Now, I'm interested in running this program on an HTML page. Can anyone provide guidance on how to accomplish this? ...

:root variables not being recognized in SCSS and Vue3 with Vite

I am encountering an issue with setting variables in my root SCSS file, as they are not being recognized correctly. Within my Vite configuration, I have included the following to import my styling: css: { preprocessorOptions: { scss: { additio ...

Ways to reduce the size of the background image in a select element?

Utilizing the bootstrap form-select select element : <select class="form-select listbox_length_menu_datatable" <option value='5'>5</option> <option value='10'>10</option> <option value ...