Highlighting menu borders and images upon loading of the page

Currently, I am working on creating a unique menu design where the initial link is highlighted with a border-bottom when the page loads. Additionally, there is an image that follows the mouse when hovering over each link in the menu.

Although I have successfully implemented the border-bottom effect on page load, I would like to display the initial image as well. My jQuery skills are limited, so the code may not be very organized.

Furthermore, I aim to make the border disappear (fade out/transition) after a user hovers over an item. Currently, the last hovered item remains underlined even when the mouse is elsewhere.

$(".menu li a").hover(function() {
  $('.menu li a').removeClass('one');
  $(this).addClass('one');
});

$(document).ready(function() {
  $('.one').mousemove(function(e) {
    $img = $("#" + $(this).data('image-id'))
    $img.stop(1, 1).show();
    $img.offset({
      top: e.pageY - 150,
      left: e.pageX + 100
    });
  }).mouseleave(function() {
    $img = $("#" + $(this).data('image-id'))
    $img.hide();
  });
});

$(document).ready(function() {
  $('.two').mousemove(function(e) {
    $img = $("#" + $(this).data('image-id'))
    $img.stop(1, 1).show();
    $img.offset({
      top: e.pageY - 150,
      left: e.pageX + 100
    });
  }).mouseleave(function() {
    $img = $("#" + $(this).data('image-id'))
    $img.hide();
  });
});

$(document).ready(function() {
  $('.three').mousemove(function(e) {
    $img = $("#" + $(this).data('image-id'))
    $img.stop(1, 1).show();
    $img.offset({
      top: e.pageY - 150,
      left: e.pageX + 100
    });
  }).mouseleave(function() {
    $img = $("#" + $(this).data('image-id'))
    $img.hide();
  });
});
* {
  margin-top: 80px;
  margin-left: 20px;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

li {
  padding: 0;
  margin: 0;
}

ul.menu a {
  color: black;
  text-decoration: none;
  font-family: sans-serif;
  transition: all 0.3s ease;
}

.one {
  color: black;
  border-bottom: 3px solid lightgreen;
}

img.mouse-hovered {
  display: none;
  position: absolute;
  width: 300px;
  height: auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul class="menu">
  <li>
    <img id="img-one" src="http://sereedmedia.com/srmwp/wp-content/uploads/kitten.jpg" class="mouse-hovered">
    <a href="#" class="one" data-image-id="img-one">One</a>
  </li>
  <li>
    <img id="img-two" src="http://www.warrenphotographic.co.uk/photography/bigs/36522-Tabby-kitten-white-background.jpg" class="mouse-hovered">
    <a href="#" class="two" data-image-id="img-two">Two</a>
  </li>
  <li>
    <img id="img-three" src="http://www.warrenphotographic.co.uk/photography/bigs/11406-Ginger-kitten-rolling-on-its-back-white-background.jpg" class="mouse-hovered">
    <a href="#" class="three" data-image-id="img-three">Three</a>
  </li>
</ul>

To view the live example, visit this fiddle: https://jsfiddle.net/d7oj35wm/

Answer №1

To display the first image without hovering, just insert this code snippet inside the $(document).ready() function.

Make sure to apply appropriate CSS styles for proper image positioning.

    
// Display initial image
$("#" + $('.one').data('image-id')).stop(1, 1).show();
    

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

Steps to activate the parent list item when the child item is altered

As a newcomer to both ui router and angularjs, I'm encountering a specific issue: Within my header section, the following code is present: <li ng-class="{active: $state.includes('settings')}" id="header01"> <a ...

Is it possible to successfully pass a parameter from a servlet to a JavaScript function, but encounter issues when trying to view the database

In my view servlet, I am displaying user data from the database in a table. Each row of the table has buttons that allow users to change the cells in that row to text boxes. The issue I am encountering is that when I retrieve the data and loop through to ...

Why isn't the state change in a React component affecting the rendering after it transitions from the constructor to componentDidMount?

I have a component that looks like this: class GettingStarted extends React.Component { constructor(props) { super(props); this.state = { measureCompletion: '0%', }; } componentDidMount() { this.setState({ mea ...

What is the method for implementing drag and drop functionality for image sorting in ReactJS without relying on external packages?

When working with touch events on mobile, I have encountered an issue: const [draggedImage, setDraggedImage] = useState(null); const handleTouchStart = (index) => { setDraggedImage(index); }; const handleTouchMove = (e, index) => { e.p ...

The function $http.get in AngularJS is providing an undefined response

In the process of developing a small Angular application, I started with this seed project: https://github.com/angular/angular-seed. The only modifications I made were to the following files: /app/view1/view1.js 'use strict'; angular.mod ...

Setting the Date in Angular.js: A Step-by-Step Guide

Make maxDate selectable as today at the latest (past days are clickable but not tomorrow). The range between minDay and maxDay should not exceed 365 days, allowing for fewer days to be selected. $scope.dateOptions = { formatYear: " ...

Is it common practice to define an object's methods before its constructor in JavaScript?

Can class methods be defined before the constructor in Javascript? For example: MyObj.prototype.fcn = function () {}; MyObj = function () {}; I am looking to keep instance methods in a separate file, but need them to load before the constructor. An ide ...

Transition property in CSS not functioning properly

In my webpage, I have div elements with multiple classes for easy manipulation in jQuery based on their nature. When I try to expand and collapse the div on a button click event, everything works fine except for the transition effect which is not working ...

What is the best way to activate a hyperlink using jQuery without causing the page to reload?

I'm in the process of designing a tumblr theme and have added a like button to each post. A demo URL looks like this: <a class="loveit" href="http://www.tumblr.com/like/fGKvAJgQ?id=16664837215">like it</a> The current setup works fine, b ...

Employing ng-style for establishing background when the value does not align

Greetings all! I am using ng-repeat to create a list of items within a ul element. Within the json data, there is a specific value that I want to highlight if it does not match a predefined string. Here's what I have so far: ng-style="{'backgro ...

Guide to converting a form into a structured object format (with a tree layout)

Looking to transform a form into an object structure? <form> <input type="text" name="Name" /> <input type="checkbox" name="Feature.Translate" /> <input type="checkbox" name="Feature.Share" /> <input type="submi ...

Basic AngularJS application, however I am receiving {{this is supposed to be the information}}

Building an angularjs app I have set up an asp.net mvc4 application and integrated the angularjs package using nuget. The Layout.cshtml file has been updated to look like this: <!DOCTYPE html> <html ng-app="myApp"> <head> <meta ...

Error message while running jQuery 3.2.1: issues with UL and LI

I have a link that displays the desired outcome Despite using jquery version 3.2.1, when I insert my code into Dreamweaver cc 2018, it fails to work. On my jsfiddle, the selection is sorted alphabetically, but on my web page it is not. var mylist = $( ...

Skip a single test from a suite in Firefox using Protractor automation framework

I have a collection of tests in my tests folder, all named with the convention ending in spec.js. By using the */spec.js option in the Config file, I am able to run all tests seamlessly. However, I encountered an issue where I needed to skip running a spe ...

Simple JavaScript numeric input field

Hey there, I'm a beginner learning JavaScript. I'm currently working on creating a program that adds numbers input through text fields. If you want to check out the HTML code, visit this link on JS Fiddle: http://jsfiddle.net/fCXMt/ My questio ...

Issue with GMap Compatibility on Specific Web Browsers

I'm currently facing an issue with implementing gMap [1] on a website. The map is functioning properly in Chrome and Safari, but it fails to display in Firefox, IE, and Opera (latest versions of all). I have ensured that the Google Map API key loads a ...

Using THREE.js to create interactive clickable objects is a fun way to engage users. By clicking on a cube, you can trigger the appearance of a second cube,

I have a goal in mind. When the first box is clicked, I want a second box to appear. Then, when the second box is clicked, I want the color of the original cube to change. I've managed to achieve the first step, but I'm unsure how to implement th ...

leaving code block using Express and Node

My code is displayed below: // Implement the following operations on routes ending with "users" router.route('/users') .post(function(req, res, next) { var user = new User(); user.username = req.body.username; user.p ...

Filter ng-repeat using OR condition for property

I'm trying to filter a list based on a model value entered in a text box. For example: var person={}; person.Id=1; person.Name="Test 1"; person.PetName="Rest 1" var persons=[]; persons.push(person); person.Id=2; person.Name="Test ds"; per ...

What causes the width of a card to vary between Windows and a Macbook Pro?

Here is the code I have: <div id="app"> <v-app id="inspire"> <v-content> <v-container> <v-card max-width="1000" class="mx-auto" > <v-form v-model="valid"> ...