Tips for displaying play and pause text when hovering

In my demo application, I have successfully created a Play and Pause icon. However, there is an additional requirement - I need to display the text PLAY and PAUSE when hovering.

Currently, I am encountering two issues:

  • The icon jumps when hovering over the button
  • The PLAY or PAUSE text is not centered (it appears slightly below)
  • I also need to display different text depending on the state

To see the code, please visit: https://jsbin.com/qifigeruti/3/edit?html,css,js,output

.container {
  padding: 8px;
  border: 1px solid;
  display: inline-block;
}

.text {
  display: none;
}

.button {
  border: 0;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  height: 20px;
  border-color: transparent transparent transparent #202020;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: 10px 0 10px 20px;
  padding: 0
}

.button.paused {
  border-style: double;
  border-width: 0px 0 0px 20px;
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}

.button:hover span {
  display: inline;
}
<div class="container">
  <button class='button'>
      <span class="text">Play</span>
    </button>
</div>

Answer №1

To create a toggle effect for icons or text on hover, my recommendation is to utilize the Font Awesome library instead of using .png images. Font Awesome allows you to easily change icons by manipulating class names through jQuery.

  1. You can implement this functionality using jQuery's .hover method, as detailed in the documentation: https://api.jquery.com/hover/

  2. Within the .hover function, you can check if the div has a specific class by referencing the hasClass() method.

  3. Subsequently, you can remove a class using removeClass() and add another one dynamically.

If you choose to use Font Awesome, altering the class name will automatically update the displayed icon. Furthermore, toggling class names enables you to modify the associated text as well.

SAMPLE

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>

    <!--===============================================================================================-->
    <link href="../Assets/Fonts/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet" />
    <!--===============================================================================================-->
    <script src="../Scripts/JQuery/jquery-3.2.1.min.js"></script>
    <!--===============================================================================================-->
    
    
    <style>
        #icon {
            width: 100px;
            height: 50px;
            border: 1px solid #000;
            font-size: 25px;
            display: flex;
            align-content: center;
            align-items: center;
            padding: 5px;
        }

        
    </style>
    
    <script>
        $(document).ready(function() {

            $("#icon").click(function() {
                if ($("#icon i").hasClass('fa-play')) {
                    $("#icon i").removeClass("fa-play").addClass("fa-pause");
                    $("#icon span").text('pause');
                } else {
                    $("#icon i").removeClass("fa-pause").addClass("fa-play");
                    $("#icon span").text('play');
                }
                

            });

        });
    </script>


</head>
<body>


    <div id="icon">

        <i class="fa fa-play" aria-hidden="true"></i>
        <span>Play</span>
        
        

    </div>


</body>
</html>

Answer №2

You have the option to align it using flex and add a transition effect to the container like so:

function onToggle() {
  document.getElementById("clicked").classList.toggle("button");
  document.getElementById("clicked").classList.toggle("pauseicon");
}
.container {
  padding: 10px;
  border: 1px solid;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s linear;
}

.text-1,
.text-2 {
  display: none;
}

.button {
  border: 0;
  background: transparent;
  box-sizing: border-box;
  width: 0;
  height: 20px;
  border-color: transparent transparent transparent #202020;
  transition: 100ms all ease;
  cursor: pointer;
  border-style: solid;
  border-width: 10px 0 10px 20px;
  padding: 0
}

.button.paused {
  border-style: double;
  border-width: 0px 0 0px 20px;
}

.pauseicon {
  border-left: 10px solid #000;
  border-right: 10px solid #000;
  width: 10px;
  height: 25px;
}

.button:hover {
  border-color: transparent transparent transparent #404040;
}

.button:hover+.text-1 {
  display: inline;
}

.pauseicon:hover~.text-2 {
  display: inline;
}
<div class="container">
  <button class='button' id="clicked" onclick="onToggle()"></button>
  <span class="text-1">Play</span>
  <span class="text-2">Pause</span>
</div>

Check out the Demo

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

What is the best way to allow text input in a table cell to exceed the confines of the table when it is clicked on

I am currently working on a table that has three columns: a label, a dropdown selector, and an input field. The challenge I'm facing is that the input field needs to accommodate multiple lines of content, specifically in JSON format. However, I want ...

Is there a way to align text without any vertical spacing between lines?

Is there a way to remove the space between these two text boxes? https://i.sstatic.net/MFkuQ.png I attempted using a container with display:flex; in it, but that caused the text to be off-center. body { background-color: lightblue; } h2 { color: ...

Error: React unable to access the 'title' property as it is undefined

I'm trying to access an array from the state in my App Component, but for some reason it's not working. import React from "react"; import "./App.css"; //import Category from "./components/Category"; class App extends React.Component { const ...

Mastering the Art of Binding Option Data from API Responses in Vue.js

Just starting out with Vue.js and attempting to bind option data from an API response. I've made an axios call in the mounted() hook and assigned the companies data from the response, but I'm encountering errors as shown below. 373:11 error ...

Displaying React components using Bootstrap in the navigation bar

I'm feeling a little stuck here. I'm currently working with React Bootstrap and the Navigation component. One of the routes in my application is the dashboard, which is only accessible after logging in. When a user navigates to the dashboard, I ...

What is the best method for displaying file size in a user-friendly format using Angular 6?

Imagine having this snippet in an HTML file: *ngFor="let cell of dateFormat(row)">{{cell | filesize}}</td>, where the dateFormat function looks like this: dateFormat(row:string){ var today = new Date(row[4]); let latest_date = this.date ...

Achieving successful ratings for various items

Struggling to implement star ratings for multiple items, my code seems to be mixing up the ratings. Here's a snippet of the HTML: <div class="thumbnail"> <img src="https://images.unsplash.com/photo-147031 ...

Adjust parent div size automatically when child is resized - jQuery Resizable Plugin

I need some assistance with jQueryUI Resizable. How can I resize only the height of the parent div when the child div is resized and touches the bottom end of the parent div? Additionally, I want to ensure that the child div does not go outside the boundar ...

Using jQuery to store the last selected href/button in a cookie for easy retrieval

Recently, I've been working on a document that features a top navigation with 4 different links. Each time a link is clicked, a div right below it changes its size accordingly. My goal now is to implement the use of cookies to remember the last select ...

Attempted to create a node index.js file but encountered an error

Whenever I try to run express node.js on "localhost:3000", I keep getting an error message in my hyper terminal that says "unexpected token =". Here is the code snippet: //JS hint esversion:6 const = require("express"); const app = express(); app.get("/ ...

Why is the Twitch api map function returning nothing, while the console log is showing output?

Presently, my Nextjs page is making multiple Twitch API calls successfully and displaying the correct data. However, one of the mapping functions is failing to render anything on the page, even though the console log shows the data. Although I am relativel ...

"CSS background not loading properly on jQuery infinite scroll feature, affecting the loading of

Currently, I am using the most recent version of infinite-scroll developed by paulirish. Everything seems to be working correctly, but there is a peculiar issue that I have encountered. Consider this scenario: You have a forum where infinite scroll is imp ...

What is the reason behind the absence of hoisting in C#?

Every day, I work with both Javascript and C#. One thing that I often have to take into consideration when working with Javascript is hoisting. However, it seems that C# does not implement hoisting (as far as I know), and I can't seem to understand wh ...

Is it possible to open a new tab window using a radio button?

Looking for assistance with radio buttons. I want users to be redirected to a new window when they click the submit button beneath the radio buttons. If anyone has a solution, please help! I have tried using window.location.href and window.open but the c ...

Revamping the Bootstrap admin template

Is there a way to customize this Bootstrap 3 admin template? https://getbootstrap.com/docs/3.3/examples/dashboard/ I want to make some changes like removing the top fixed navbar and shifting everything up by 50px (as defined in dashboard.css). However, I ...

Tips on effectively utilizing a value that has been modified by useEffect

Here is my current code: const issues = ['x','y','z']; let allIssueStatus; let selectedIssueStatus = ''; useEffect(() => { const fetchIssueStatus = async() => { const response = await fetch(&ap ...

Problem with Flickity's is-selected feature

I am currently exploring Flickity and its functionality. I have a carousel that auto-plays, with the selected cell always placed in the middle and highlighted with a grey background. However, I would like to customize it so that the selected cell is positi ...

Enabling hover effects to scroll divs only when interacted with

I am aiming to achieve two separate scrolling divs and I'm uncertain about the exact approach. Experimenting with various overflow properties has only resulted in one scrolling independently. .profile { width: 100%; display: flex; ...

When it comes to utilizing the method ".style.something" in JavaScript

Here is some javascript code that I am working with: function createDiv(id){ var temp = document.createElement('div'); temp.setAttribute("id", id); document.getElementsByTagName('body')[0].appendChild(temp); } c ...

Managing the display of an extensive list of items in react - best practices

I'm facing a challenge with performance as I have a significant number of items to render and the data is constantly being updated in real-time via socket-io. How can I optimize for performance when the table of items is being rendered frequently due ...