I would like to add a similar drop-down input field (picture below), to my website. I want it to appear when a link/image/button is clicked on. Can this be done with JavaScript? Also, I don't want it to affect the flow of the page.
Can this be done with JavaScript?
Yes. The JavaScript/CSS equivalent would be a button which (when clicked) displayed an element with position: fixed
or position: absolute
.
Here is a simple example using jQuery: http://jsfiddle.net/Byujd/1/
This is a bit complex.
You need an element which is displayed above the content:
<div class="modal">
<!-- Some content like textarea and stuff -->
</div>
Now you can style this box with the position
-property and other things:
.modal {
position: absolute;
z-index: 999;
}
This now displays above the rest of the content.
The button just needs a listener. With jQuery this looks something like this:
$('.the-buttons-class').click( function (e) {
e.preventDefault();
$('.modal').toggle();
});
Instead of toggling the state of the modal with jQuery's toggle()
-function you could toggle a class on it and manipulate the state accordingly:
CSS:
.modal {
...
display: none;
}
.modal.shown {
display: block;
}
JS:
$('.the-buttons-class').click( function (e) {
e.preventDefault();
$('.modal').toggleClass('shown');
});
I've done something pretty similar at my page . Make sure to check the search box. The code for it is on GitHub .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.