I'm having trouble with two JavaScript functions interacting - the second one seems to be interfering with the first. How can

I'm facing an issue with my webpage that includes two distinct features:

1) A button that changes feelings on hover, similar to Google's "I'm Feeling Lucky" button

2) An automatic word cloud generation on page load

Oddly, the button functionality ceases to work when I implement the word cloud feature. Despite checking for repeated variables, I can't determine the cause of this issue. Can someone assist me? Below is my code:

// Interval and words for placing words randomly
var interval = 100; // Interval between word placements (in milliseconds)

var words = [
  'JavaScript',
  'Angular',
  'ReactJS',
  ...
];

// Get the width and height of the viewport
var width = window.innerWidth;
var height = window.innerHeight;

// Function to place words at random positions
var wordPlacementInterval = setInterval(function() {
  ...
});

$("button").hover(function(){
  ...
}, function(){
  ...
})
* {
  box-sizing: border-box;
}

/* body {
  font-family: "Arial", sans-serif;
} */

...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="top"></div>

<button>
  <ul>
    <li>I'm Feeling Lucky</li>
    <li>I'm Feeling Alone</li>
    <li>I'm Feeling Absence</li>
    <li>I'm Feeling Void</li>
    <li>I'm Feeling Deficit</li>
    <li>I'm Feeling Lack</li>
  </ul>  
</button>

If anyone has suggestions on how I can integrate both features without conflicts, I would greatly appreciate it. Thank you.

Answer №1

The code is functioning correctly. The issue arises when you attempt to hover over the button, as it seems to be obstructed by <span> elements.

Instead of completely revamping your word cloud, one potential solution is to assign a z-index value to the <button> in the CSS:

z-index: 2;

For instance:

// Interval and words for placing words randomly
var interval = 100; // Interval between word placements (in milliseconds)

var words = [
'JavaScript',
'Angular',
'ReactJS',
'Three.js',
'Moment',
'Redux',
'Chart.js',
'Ember',
'BackboneJS',
'Underscore',
'Reveal.js',
'Babel',
'Modernizr',
'CoffeeScript',
'TypeScript',
'Gulp',
'NodeJS',
'Select2',
'Bower',
'NPM',
'Express',
'Pdf.js',
'Full Page.js',
'Foundation',
'Bootstrap',
'Mocha',
'Jasmine',
'D3.js',
'Mustache.js',
'Handlebars',
'jQuery Validation',
'Lodash',
'RequireJS',
'IntroJS',
'Typeahead.js',
'Scrollr',
'MooTools',
'Prototype',
'YUI',
'Dojo',
'Raphael',
'Knockout'
];

// Get the width and height of the viewport
var width = window.innerWidth;
var height = window.innerHeight;

// Function to place words at random positions
var wordPlacementInterval = setInterval(function() {
var currentWord = words.shift();
if (currentWord) {
var word = document.createElement('span');
word.innerHTML = currentWord;
// Randomly position the word within the viewport
word.style.top = Math.floor(Math.random() * height) + 'px';
word.style.left = Math.floor(Math.random() * width) + 'px';
document.body.appendChild(word);
}
else {
clearInterval(wordPlacementInterval);
}
}, interval);

$("button").hover(function(){
var self = this;
var pos = -(Math.floor(Math.random() * 5) + 1) * 55
setTimeout(function(){
$(self).find("ul").css("margin-top", pos + "px");
}, 500);
}, function(){
$(this).find("ul").css("margin-top", "0px");
})
* {
box-sizing: border-box;
}

/* body {
font-family: "Arial", sans-serif;
} */

button {
margin: 10% auto;
display: block;
padding: 0;
appearance: none;
overflow: hidden;
height: 50px;
border: #DCDCDC 1px solid;
background-color: #F2F2F2;
background-image: -webkit-linear-gradient(top, #F5F5F5, #F1F1F1);
background-image: -moz-linear-gradient(top, #F5F5F5, #F1F1F1);
z-index: 2;
}

button:hover {
border-color: darken(#DCDCDC, 5%);
box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 1px;
background-image: -webkit-linear-gradient(top, #f8f8f8, #F1F1F1);
background-image: -moz-linear-gradient(top, #f8f8f8, #F1F1F1);
}

button:active {
box-shadow: rgba(0, 0, 0, 0.2) 0px 0px 1px;
outline: none !important;
}

button:focus {
outline: 1px #08c solid;
}

button ul {
margin-top: 0;
padding: 0;
list-style: none;
-webkit-transition: margin-top .45s ease-in-out;
-moz-transition: margin-top .45s ease-in-out;
transition: margin-top .45s ease-in-out;
}

button ul li {
display: block;
height: 55px;
padding: 1em;
}

body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

span {
font-size: 12px;
position: fixed;
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="top"></div>

<button>
<ul>
<li>I'm Feeling Lucky</li>
<li>I'm Feeling Alone</li>
<li>I'm Feeling Absence</li>
<li>I'm Feeling Void</li>
<li>I'm Feeling Deficit</li>
<li>I'm Feeling Lack</li>
</ul>  
</button>

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

Issues arise when attempting to use the Android KeyUp, KeyDown, and KeyPress events in conjunction with Angular2

I am encountering an issue where I consistently receive a keyCode of 229 in Android Chrome browsers when running either: <input type="text" (keydown)="testKeyCodes($event)"/> <!-- or --> <input type="text" (keyup)="testKeyCodes($event)"/& ...

Playing out the REST endpoint in ExpressJS simulation

Suppose I have set up the following endpoints in my ExpressJS configuration file server.js: // Generic app.post('/mycontext/:_version/:_controller/:_file', (req, res) => { const {_version,_controller,_file} = req.params; const ...

Tips for incorporating a card game into an HTML platform

Seeking guidance on creating a card-based game inspired by Game of Thrones, similar to yu-gi-oh. While experienced in Java, C, C++, I am new to HTML and its libraries. Any advice on which libraries or methods to utilize would be greatly appreciated. I aim ...

GraphQL File Uploads: A Seamless Way to Upload Files using

In the process of developing my Express-GraphQL API, I have reached a stage where I am working with MongoDB. At this point, I have defined the following: Project Mongo model: const { Schema, model } = require("mongoose"); const projectSchema = new Schema ...

What is the best way to divide MySQL content (text and HTML) into multiple columns?

Before diving in, I want to acknowledge the hesitation around asking questions without first trying some trial and error. However, in this case, the need for specific knowledge is essential, so I am going to proceed with my query. I have been grappling wi ...

"Utilize parameter passing with mapGetters in the Vuex state management system

Hello there! I'm currently working on a Vue.js project and using modules. I am trying to get filtered data from a getter, but I'm unsure how to provide parameters. Specifically, I need to pass a 'name' parameter to the Getter. How can ...

Creating a stylish button using Bootstrap in an MVC Core HTML Tag Helper

I recently scaffolded a table in MVC and created a view. I would like to customize the Edit button by changing it to a blue Bootstrap button. How can I accomplish this? Existing Code: <td> <a asp-action="Edit" asp-route-id="@ ...

Is it possible to justify align inline-block elements across multiple lines?

I have encountered various scenarios where I need a DIV to contain an inline-block menu element - usually an anchor - and behave as if the elements are vertically justified, even when they overflow onto multiple lines. Let me illustrate with an example: ...

Ways to extract only the highest-ranking entries with closely matching values in a designated column?

Imagine we have a DataTable containing 1000 rows. Each row in Col_1 consists of two numbers. Below are the first five records: Col_1 Col_2 10-01 Alex 10-02 John 10-04 Sara 20-07 David 20-09 Will . . . . . . Is there a way to create a filte ...

Execute a function in wxHtmlWindow after the page has finished loading

I'm currently facing an issue with wxPython that I could use some help with. My goal is to create a basic HTML window with forward and backwards navigation buttons. However, I'm encountering difficulties when users click on links within the page. ...

Loop through the JSON data and dynamically display corresponding HTML code based on the data

I've developed a survey application using React, where I initially wrote code for each question to display radio buttons/inputs for answers. However, as the survey grew with multiple questions, this approach resulted in lengthy and repetitive code. To ...

What is the best way to ensure that the text on the sub menus in the recipes navigation bar is displayed in vibrant colors?

Can anyone help me figure out how to make the blue hover effect fit perfectly with the subheadings in my recipes menu? Right now, the color doesn't cover the text fully. Feel free to read and run my code for a better understanding. This is the CSS I ...

The Navbar design in Bootstrap is not scaling properly with the background image, making

I am currently in the process of developing a Bootstrap 5 webpage based on a provided design, but I am encountering some challenges. Here is the design I am working from: https://i.sstatic.net/MsFzq.jpg Here is my current progress: https://i.sstatic.ne ...

Retrieving information from MongoDB for a specific ObjectID associated with the current authenticated user

Having two collections in my MongoDB structured as follows: Data User: id: ObjectId ("5fb39e3d11eaad3e30cfb1b0") userName: "Tobias" password: "yyy" id: ObjectId ("5fb3c83fb774ff3340482250") userName: "Thor&qu ...

Create a compressed package of a Vue project that can easily be inserted into a Blogger blog post as a single HTML file

Is there a way to package all the files related to a Vue.js project (HTML, JavaScript, CSS) into one single HTML file for easy deployment on a Blogger Blogspot post? In the past, a question similar to this was asked regarding bundling files into a single ...

Instructions on changing class when input length is not equal to zero

Is there a way to dynamically add a class to an input[type="email"] when its length is greater than 0 and remove it when the input length reaches 0? I'm not very proficient in JavaScript, so the solution could be implemented using vue.js or pure Java ...

Using PHP to create dynamic text within email messages

I'm currently working on creating dynamic email content, and I have successfully implemented a solution for images. However, I am facing difficulty when it comes to displaying text dynamically. The PHP code snippet below retrieves location data and r ...

Place the div at the bottom of the page or viewport if it is bigger

Is there a way to ensure that a div with a background remains at the bottom of the page, regardless of whether the content on the page pushes it down or not? Currently, I am using the following code: #bottomBar { position: absolute; bottom: 0; } ...

When a user hovers over an element, display text in a particular div

I've been experimenting with different methods to achieve this, but nothing seems to be working for me. The idea is that when I hover over a specific div like "divOne" or "divTwo", I want a text to appear in the "writeToMe" div. After spending several ...

Managing a promise and using async/await functions

Having an issue with my node and express function router.post('/', async (req, res) => { const playlist = new Playlist({ song: req.body.song, artist: req.body.artist }) try { const newPlaylist = await play ...