When clicking, populate the content of another div in a vertical top-to-bottom fashion

I am in the process of creating a functionality where clicking on one element will fill another element's content from bottom to top.

Although I have managed to make it work on hover, the same functionality is not triggered when clicking on the element itself.

$(document).ready(function(){
    $('.click-me').on('click', function(){
       $('.button-grey').trigger('hover');
  })
})
.button-grey {
    padding: 13px 40px;
    display: inline-block;
    border: 1px solid #969496;
    position: relative;
    text-decoration: none;
    font-weight: bold;
    text-transform: uppercase;
    font-size: 11px;
    background-image: linear-gradient(to top, #fff 50%, #969496 50%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    color: transparent;
}
.button-grey:before {
    content:"";
    z-index:-1;
    position:absolute;
    top:-1px;
    left:0;
    right:0;
    bottom:0;
    background-image: linear-gradient(to top, #969496 50%, transparent 50%);
}

.button-grey,
.button-grey:before {
    background-size: 100% 200%;
    background-position: top;
    transition: background-position 0.5s ease-in-out;
}

.button-grey:hover,
.button-grey:hover:before{
    background-position: bottom;
    cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="click-me">Click me</div>
<div class="button-grey"></div>

I am currently exploring ways to achieve this desired outcome as depicted in the image: https://ibb.co/3Mq8PzL

Answer №1

It's not possible to activate hover in that manner. This method might only activate it for a brief moment (around 1ms) since there is no actual hovering occurring. As a result, nothing will be visible.

An alternative approach would be to utilize a class and toggle it upon clicking:

.button-grey_fakehover,
.button-grey_fakehover:before{
    background-position: bottom;
    cursor: pointer;
}

In this instance, we are defining styles for a class named button-grey_fakehover (but feel free to choose any name).

$(document).ready(function(){
    $('.click-me').on('click', function(){
       $('.button-grey').toggleClass('button-grey_fakehover');
  })
})

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

The onchange() function for a multi-checkbox dropdown is failing to work as

Issue with Multiple Drop Down Checkbox Functionality: The first parameter is always received as undefined, and the onchange event only captures the value of the first checkbox selected. <select name="multicheckbox[]" multiple="multiple" class="4colacti ...

Aligning text vertically in the center using Bootstrap

How can I center text vertically within a division with a height of 100vh? Here's what I've tried: .about-header { height: 100vh; background: #000; } .about-header p { font-size: 5em; } <link rel="stylesheet" href="https://maxcdn.boot ...

Browsing through different pages on a Rails app will trigger numerous requests to various destinations

My application consists of a landing page and a profile page. The relevant parts of my rake routes are: index GET /welcome/index(.:format) welcome#index show GET /profile(/:username)(.:format) sessions#show root / ...

Troubleshooting Alignment Problems in Bootstrap 4 Tables

Having trouble aligning certain items. In thisFiddle, I have two options: Option1 and Option2. I want to align the toggle switches with the ones in the first two rows. Specifically, I want the toggle switches to be in the second column of the table and t ...

Tips for ensuring that hover:after contents do not impact the positioning of the next element

I have created a photo list section on my webpage. <div id="main_post_photo"> <% for(var q=0;q<resolve.length;q++){ if(resolve[q].images.length>=1){%> <a href='/post/<%= resolve[q]._ ...

Implement jQuery Tabs in Brackets software to enhance user experience

My Adobe Creative Cloud subscription is expiring soon, and I am considering switching to Brackets, an open-source code editor developed by Adobe. However, I am facing some difficulties adding jQuery tabs to my HTML documents. I downloaded the 1.10.4 zip f ...

transition effect of appearing and disappearing div

Having trouble creating a fade out followed by a fade in effect on a div element. The fade out happens too quickly and the fade in interrupts it abruptly. Here is the JavaScript code: $('#fillBg').stop(true,false).fadeTo(3000, 0); $("#fillBg"). ...

How to position slides in the center using react-slick

I'm having difficulty achieving vertical centering for an image in a react-slick carousel implementation. The issue can be seen here. https://codesandbox.io/s/react-slick-playground-o7dhn Here are the problems identified: Images are not centered: ...

Guide to combining parameters prior to making an ajax request

Here is a code snippet that works: $('#someId').on('click', function() { var myData = '{ Periode: "something" }' $.ajax({ type: "POST", url: "/Nyhedsbrev/SendMailKunTilMig", contentType: &apos ...

Hovering over the Instagram icon will reveal a stunning visual effect

I am looking to change the appearance of my Instagram icon when hovering over it. I want the background to turn white and the icon to become colored. Here is my code snippet from Footer.js : <a href="https://www.instagram. ...

The brief interruption in ASP.NET SignalR communication results in the user being temporarily disconnected, and quickly reconnected

Is it possible to maintain a connection when the user navigates to another subpage? I am using @Url.Action("Action","Controller") I have a JQuery click function attached to Url.Action which triggers a signalR hub server method. However, I am facing an is ...

Hide the burger menu when a link is clicked

Is there a way to make my Bootstrap burger menu automatically close when the user clicks on a menu link, rather than having to click on the burger itself? I've tried using some JavaScript code but it ends up disabling the burger menu entirely. Can any ...

Having trouble bringing in css files

I'm having trouble loading a CSS file when trying to import it into another CSS file. I've tried various ways of defining the path but nothing seems to work. Any insights on what might be causing this issue? https://i.sstatic.net/IwaY1.png ...

Tailwind CSS - when it comes to styling, larger screen media queries are taking precedence over smaller media queries

Currently tackling screen responsiveness issues in a Nextjs + Tailwind CSS project. All Tailwind breakpoints are functioning correctly except for "sm" and below. Snippet of the problematic code: <div className="bg-red-200 sm:bg-white md:bg-gray-70 ...

Animating the background position in jQuery is not working

var items = $('#menu-top-menu').children(); items.hover(function(){ var itemPosition = $(this).position().left; var parentPosition = $(this).parent().position().left; var backgroundPosition = itemPosition - parentPosition; $(&apos ...

Learn the process of utilizing Javascript to substitute additional texts with (...) in your content

I am facing a challenge with a text field that allows users to input text. I want to use JavaScript to retrieve the text from the textbox and display it in a paragraph. However, my issue is that I have set a character limit of 50. If a user exceeds this li ...

Update the FontSize in the HTML dropdown menu using JavaScript

I am having trouble filling my Selection using a script. For example, when I try to populate my FontSizeMenu, I use the following code snippet: function FillFontSizeMenu() { FillSelection(GetPossibleFontSizes(), "fontSizeMenu"); } function GetPossib ...

Retrieve the innerHTML contents from all span elements

I'm having trouble getting the content of all span tags that are child elements of div#parent. So far, I've only been able to retrieve the content of the first one. Can someone please assist me with this? $( document ).ready(function() { ...

Display the item with jQuery once the CSS generated by jQuery has finished loading

Is it possible to delay the visibility of an object until my script finishes loading? I tried using ajaxcomplete() and ajaxSuccess() without success. Check out the code snippet below for an example. There's an image with the id: imagefocus. Whenever ...

Attempting to design a customized dropdown navigation feature using HTML

I am having some trouble creating a dropdown menu that functions properly. Here is the code I have written: <div> <H1><FONT="TIMES ROMAN" FONT-COLOR="BLUE" > SELECT A Subject:</H1> <select id ="dropDownId"> <!-- as ...