Switch the input direction from left to right

Check out my demonstration that is currently functional: JSFIDDLE

$('#btn-search').on('click', function(e) {
    e.preventDefault();
    $('#search').animate({width: 'toggle'}).focus();
});

Is there a way to modify this so that when clicked, the input expands to the left instead of the right? I am looking to have the input expand towards the left side upon clicking.

Answer №1

Check out this live demonstration: http://jsfiddle.net/7tq9146a/1/

Implemented float:right in place of float:left and adjusted the sequence of li elements.

Answer №2

It seems like you're looking to create an animation for the input field where it expands gradually, like this:

             |[button]
         |<--|[button]
       |<----|[button]
    |<-------|[button]

Instead of simply shifting the button as follows:

             |[button]
             -->|[button]
             ---->|[button]

To achieve this effect, you need to adjust both the width and left positioning of the input element. Currently, the animate function is only changing the width without moving the input's starting position. Therefore, you also need to shift the button along with the input to accommodate its increased width.

To implement this behavior, you can modify the code as shown below:

        $('#btn-search').on('click', function(e) {
          e.preventDefault();
          $('#search').animate({
              width: 'toggle', 
              left: "-=177", // Adjust based on the input element's width
          }).focus();
          $('#btn-search').animate({left: "-=177"});
        });

Keep in mind that there may be additional adjustments required when following this approach. For a more comprehensive guide, check out this article on managing the button and input element using different JavaScript and CSS techniques: http://tympanus.net/codrops/2013/06/26/expanding-search-bar-deconstructed/

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

Having trouble with pm2 starting up correctly?

I have encountered an issue while trying to launch a nodejs application in pm2 on bluehost shared hosting. When I run the command pm2 start ./bin/www, the server fails to start and displays the following message: [PM2] Spawning PM2 daemon with pm2_home=/h ...

Exploring CSS shaders in a browser?

Which browser out there fully supports CSS shaders? ...

Is it possible to program Zapier to automatically open a hyperlink that was included in an email?

Currently, I am utilizing Mailparser for extracting information from a booking email, and then leveraging Zapier to input this booking into our system. Within the email, there exists a link that needs to be accessed in order to confirm the booking. I am i ...

Develop a custom time input mask in AngularJS controller

In my AngularJS controller, I have the following code snippet: $scope.detailConfig = [{ title: $filter('translate')('bundle.app.HORA_MINUTO_INICIAL_DESCONSIDERAR'), property: 'faixaHorariaInicial', type: ' ...

Adjust the size of the canvas element based on changes to its parent's dimensions

I am working with an application that includes a div containing a canvas element to display an image. Additionally, there is a sidebar that can be hidden with the click of a button, causing all other elements to resize and adjust to the remaining space. W ...

Steps for updating a text section beneath a carousel

I'm looking to enhance my webpage with a bootstrap carousel, which is pretty standard. However, I want the content under each slide to change dynamically, but I'm struggling to make it work. As a newbie, I could use some guidance. I've atte ...

When clicking on the Bootstrap navbar after implementing the onclick functionality, the color changes to

I was having no issues with my navbar until I introduced an onclick function to it. Here is the code for my onclick function: $('.nav li a').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/ ...

Can you explain the significance of 'height: 0;' in CSS coding?

I've recently come across some outdated CSS code that sets the height to height: 0; in multiple places. I'm curious about what this 0 signifies and what unit it is measured in. If anyone could provide some insight, I would greatly appreciate it. ...

React - updating key prop does not trigger re-render of child component

I have implemented react-infinite-scroll to display a continuous feed of data. My goal is to refresh the data feed whenever the user makes any changes to their settings. To achieve this, I am utilizing a key prop for the InfiniteScroll component. The conc ...

Ways to test the initial launch of an Android application using Cordova

I am developing an Android application using Cordova. My app consists of multiple pages, with the main homepage being index.html. I need to determine if it is the first time a user lands on the homepage after opening the app, regardless of how many times ...

The jQuery click event is failing on the second attempt

My PHP code dynamically generates a list, and I want to be able to delete rows by clicking on them. The code works fine the first time, but not the second time. HTML <li> <div class="pers-left-container"> <img src="<?php ech ...

Add the file to the current directory

As a newer Angular developer, I am embarking on the task of creating a web page that enables users to upload files, with the intention of storing them in a specific folder within the working directory. The current location of the upload page component is ...

css: positioning images with overlap in a responsive layout

Two divs have been created with background images, both displaying the same image but in different colors - one grey and the other green. These images are waveforms. Initially, only the grey image (div1) is visible while the green image (div2) remains hid ...

Unexpected behavior encountered with jQuery code in HTML

I am attempting to add new li elements with img element inside a ul based on an array. However, only the first item in the array is being inserted. $.getJSON('/test-url/123').done (data) -> $.each(data, (index, value) -> $('.col ...

When hovering or mousing over, the text remains stationary and does not follow the scroll bar movement within the

A method I am using involves displaying additional data on hover for a shortened header, like this: Hover behavior However, the text shifts across the page when the scrollbar is used, as shown here: Scrollbar interaction This table showcases HTML, CSS, ...

Tips for including the % symbol in the Y-axis labels on a HighChart graph

I am attempting to incorporate the % symbol after the value of 100 or -100 on the yAxis in the chart shown above. I made an attempt to add the % symbols as follows: quotes.data.frequency_counts[i].negative = Math.round(negative * -1)+'%'; quote ...

Executing a save function in the controller when the TinyMCE save button is clicked through an Angular directive

For the Rich text editing in my angularjs application, I am using the TinyMce angular directive. The directive is working perfectly as expected. However, I wanted to include the save plugin to enable custom save functions. To integrate the save plugin, I ...

Toggle the JavaScript on/off switch

I am attempting to create a toggle switch using Javascript, but I am encountering an issue. Regardless of the class change, the click event always triggers on my initial class. Even though my HTML seems to be updating correctly in Firebug, I consistently ...

Determine the size of an SVG while taking into consideration any strokes applied

Is there a way to seamlessly position an SVG in the corner of a div, despite having a dynamically generated stroke? Calculating the distance to the outermost part of the border proves difficult when dealing with irregular shapes like stars. Here's my ...

What steps can a web developer take to view user console errors?

Is there an effective method for a web developer to receive notifications of console errors logged by other authorized users? I am currently working with Express, Passport, and MongoDB. These errors may occur on either the client or server side. One approa ...