Switch between two fields using just one animation

I've been experimenting with creating an information tag that slides out from behind a circular picture. To achieve this effect, I created a block and circle to serve as the information field placed behind the image.

However, I'm facing a challenge in making it slide out smoothly. Due to having two div elements, the square slides out first followed by the circle, resulting in a choppy transition.

My goal is to make it toggle in and out seamlessly, appearing as one cohesive object.

$(document).ready(function(){
    $('.employeeBlock').hide();
    $('.employeeDot').hide();
    $('.employee').click(function(){
        $('.employeeDot').toggle('slide');
        $('.employeeBlock').toggle('slide');
}); 

I've attempted placing the employeeDot inside the employeeBlock within the employee div, as well as keeping them separate but within the employee div.

Both methods yield similar outcomes.

Thank you

EDIT: Thank you for the suggestions. The animation is smoother now, but there's still room for improvement. I believe I need to create a unified item with a bullet-like shape, toggling it in and out. Any thoughts on how to accomplish this?

The best I can achieve currently is a pill shape, which leaves some areas uncovered.

EDIT: Below is my HTML structure:


    <body>
        <div class='employee'>
            <div class='employeeDot'></div>
            <div class='employeeBlock'></div>
            <img class='pic' src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQfMDb1Qtu7gTDZTfnFR2XcPqrfkn27zeWASTBfczi-GGQAIKG_"/>

        </div>      
   </body>
</html>

And here is my CSS:

.pic { height: 150px; width: 150px; border-radius: 75px; position: absolute; }


.employeeBlock {
background-color: maroon;
height: 150px;
width: 150px;
position: absolute;
left: 75px;
float: left;
}

}


.employeeDot {
background-color: maroon;
height: 150px;
width: 250px;
border-radius: 150px;
position: absolute;
float: left;
left: 75px;
}

}

Answer №1

By combining multiple selectors, you can achieve a unified outcome:

$('.productImage, .productDescription').fadeIn('slow');

Multiple Selector

Answer №2

.toggle(); has been marked as outdated, please replace it with .slideToggle();.

Slide down:

$(document).ready(function(){
    $('.employeeDot','.employeeBlock').hide();
    $('.employee').on("click", function(){
        $('.employeeDot, .employeeBlock').slideToggle('fast');
    });
});

Slide from side:

$(document).ready(function(){
        $('.employeeDot','.employeeBlock').hide();
        $('.employee').on("click", function(){
            $('.employeeDot, .employeeBlock').animate({width: 'toggle'});
        });
    });

Answer №3

All aspects of the question have been addressed by other responses, however, for achieving a bullet-like shape on the element, apply the following CSS:

border-radius: 10px 10px 0 0;

Feel free to adjust the values and sides according to your preferences.

Answer №4

Give this a shot.

$(document).ready(function(){
$('.staffBlock,.staffDot').hide();
$('.staff').click(function(){
    $('.staffDot, .staffBlock').toggle('slide');});

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

Creating a customizable theme for a website built with Bootstrap, incorporating changes to brand colors and more, by utilizing a .less file

I have built my asp site using bootstrap as the client-side CSS framework. I am currently working on implementing a feature that allows users to customize their site's brand color and other aspects, with these changes being saved permanently. While I ...

When attempting to upload a file using ajax, the $_FILES variable in PHP came

I am facing an issue with uploading images via AJAX where my PHP is not receiving the AJAX post. function handleFileSelect(evt) { files = evt.target.files; // FileList object $('.thumb-canvas' + filesId).css('display','bl ...

What are some ways to customize the default Head tag in Next.js?

After using create-next-app to create a basic page, I decided to style the index.js page. I added a navigation bar within the header and now I'm trying to customize it. I've been wondering if there's a way to style the custom Head tag in Ne ...

How can I capture the ng-click event in AngularJS when using bootstrap-select for styled select inputs?

After selecting an option, I am facing an issue where I cannot capture the ng-click event when a user chooses a value from the drop-down menu. This is because the select option has been altered by bootstrap-select, as the default select option does not sup ...

Is it possible to divide a text string into distinct arrays using spaces?

One method I am familiar with for splitting a string involves using the .split() method, like so: function split(txt) { return txt.split(' '); } When executed, this function would return ['hello', 'world'] if provided wi ...

Exploring the world of three-dimensional graphics with the power of HTML5, canvas,

Here is the HTML markup I am working with: <canvas id="container" name ="container" width="300" height="300"></canvas> This is the JavaScript code: var WIDTH = 400, HEIGHT = 300; var VIEW_ANGLE = 90, ASPECT = WIDTH / HEIGHT, NEAR = 1, FAR = ...

Discover the position of characters within a given string

I'm trying to accomplish a task similar to this: If the array [ "a", "b", "c" ] includes any of the characters in the constant word = "abracadabra", I want to retrieve that character along with its position in const word. My attempt so far looks lik ...

Enhance your UI experience with a beautifully styled button using Material-

I was using a Material UI button with a purple background. <Button component={Link} to={link} style={{ background: '#6c74cc', borderRadius: 3, border: 0, color: 'white', heig ...

Explore the world of interior CSS border styles

Trying to achieve a unique border effect like the one shown in the image. Experimented with different styles such as inset, outset, ridge, and groove, but unable to get the desired outcome. Is there a way to create a border that bends inwards towards the m ...

What is the best way to eliminate the class from the previous selection when a new item is clicked?

Here is the link to the code snippet. Is there a way to remove the class from the previously selected item when clicking on another item in this accordion menu? I would like for the class to be removed from the previous item when a new one is expanded, an ...

What is the optimal approach for managing server-side data stored as a JavaScript variable?

When dealing with global variables like JSON inserted into a web page server side with PHP, the best way to handle it is up for debate. Options might include leaving it for garbage collection, omitting var initialization and deleting the variable, or simpl ...

Vuetify Container with a set maximum width that remains fixed

I recently started developing a web application using Vue.js and Vuetify (https://vuetifyjs.com/en/). Currently, I have a basic layout with 3 columns. However, I noticed that the width of these columns is restricted to a maximum of 960px due to some defau ...

What is the procedure for selecting an element based on its child containing specifically filtered text?

Imagine a webpage with the following elements: <div data-search="type1"> any HTML <!-- .click is a child of any level --> <span class="click" data-source="page1.html">blue</span> <!-- let's call it "click1 ...

Which codec strings are considered valid for the web codecs API?

I am searching for a definitive list of valid strings that can be passed as the `codec` parameter in `VideoEncoder.isConfigSupported({ codec, width, height })`, but I have been unable to find a clear answer so far: The TS declaration file states that it m ...

How can I add navigation dots to my slider?

I've been experimenting with my slider and I managed to make it slide automatically. However, the issue is that there is no option to manually navigate through the slides. I am looking to add navigation dots at the bottom so users can easily switch be ...

Creating a dynamic drag-and-drop layout in React using react-grid-layout

Utilizing the react-grid-layout package for implementing drag-drop and resize components, I have successfully achieved the functionality outlined in the documentation. Description of My Issue My challenge lies in creating a dynamic UI where the layout is ...

Comparing HTML5 Drag and Drop to jQuery UI Drag and Drop: What's the Difference

In the ever-evolving world of web development, what is the most effective method for creating a drag and drop builder in 2017? While this question has been posed in the past (as seen here), technology changes rapidly. Is HTML5 still the go-to option, or ha ...

What is the best way to retrieve the @Url.Action url from a distinct script file?

Currently, I have organized my JavaScript file separately from my web pages. One issue I am encountering is that I am unable to write the following line of code: var url = '@Url.Action("AddTrade", "DataService")'; I hesitate to hard-code the UR ...

Adjust the CSS of the currently dragged item using jQuery sortable

I am currently using jQuery-ui sortable and facing an issue with changing the appearance of a dragged item. I would like the text value of the component inside the red background container to be displayed while dragging. Can someone please assist me with t ...

Error loading console due to JSON data on website

After creating a Json data file named question.json with the following content: "Endocrinology":[ { "title":"Endocrinology", "id": "001", "date":"08J", "question":"In adult men, anterior pituitary insufficiency does not caus ...