Ways to toggle the visibility of an element by tapping or clicking anywhere on the screen

I am creating a website that is compatible with desktop, iPad, and iPhone. On one page, I have a header and footer that are initially visible for 5 seconds after the page loads, then automatically disappear. Users can toggle the visibility of the header and footer by clicking or touching anywhere on the screen. When the header and footer are visible, the rest of the page will be dimmed similar to lightboxes.

If you want to see an example of this effect, you can visit http://jsfiddle.net/jitendravyas/yZbTK/2/

The desired outcome is to replicate the behavior seen in the default "Photos" app on iPad.

Answer №1

Here is the solution to your query. When the page loads initially, we have a fade-out effect after a certain number of seconds. If the user clicks, the toolbar will fade in if it's hidden, or fade out if it's already visible. Additionally, if the toolbar is faded in but the user doesn't interact with it for a specified time, it will automatically fade back out.

I have made some enhancements to my previous answer.

http://jsfiddle.net/yZbTK/11/

http://jsfiddle.net/yZbTK/11/show - optimized for iPad full-screen mode

To easily manipulate the controls for fading in and out, it would be beneficial to assign them a specific class. Using IDs to identify them as shown in the initial code example may not be the best approach.


var timer;
var timeVisible = 5000;
timeFadeout();

function timeFadeout() {
    timer = setTimeout(function() {
        $('.controls').fadeOut();          
    }, timeVisible);
}

$('html').click(function() {
    clearTimeout(timer);
    if ($('.controls:visible').length) {
        $('.controls').fadeOut();
    } else {
        $('.controls').fadeIn();
        timeFadeout();
    }
});

Answer №2

An easy fix is to set up a click event listener on the body element, toggling the visibility of both the header and footer:

$('body').click(function() {
    $('#header').toggle();
    $('#footer').toggle();
});

Check out this example as well: http://jsfiddle.net/Sgoettschkes/yZbTK/4/

If you want to include a "dim" effect, you could create a positioned div with opacity that toggles along with the other elements. I experimented a bit and came up with this solution: http://jsfiddle.net/Sgoettschkes/yZbTK/8/

Answer №3

UPDATED:

let timer = 5;

$('body').click(function() {
   ....
   if (timer < 1) {
       ...
   }
}

let cleanupTimer = setInterval(function() {
   timer--;
   if (timer < 1) {
       ...
       clearInterval(cleanupTimer);
   }
},1000);

That should do the trick!

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

Leveraging ng-if in conjunction with ng-bind within AngularJS

<span ng-repeat="tag in tags" ng-if="!$last"> {{tag + "," }} </span> <span ng-repeat="tag in tags" ng-if="$last"> {{tag}} </span> I have a list of tags that I want to display with commas in between. However, I need to remove ...

Ways to differentiate between buttons in a button cluster?

I have created a set of 3 buttons using the code from this resource. However, the buttons are currently positioned vertically close to each other as shown in the source code. I would like to adjust the spacing between them. Is there a way to achieve this ...

Press the button to modify the titles of the table

One of the tasks I'm working on involves a table with column titles. In this table, there's an edit button that, when clicked, should convert the title into an input field with the current title as its initial value. The edit button will then cha ...

Concealing text in multi-select option of Bootstrap when overflowing horizontally

I am attempting to create a multi-select feature with only one horizontal scroll bar using bootstrap. However, I do not want each option to have its own horizontal scrollbar. When I choose the longest option (123456789012345678901234567890), only part of ...

Creative Text Container CSS Styling

Check out this website for the container I want to replicate: container This is the specific textbox: Here's how mine currently appears: I analyzed their css file and examined their html source code. I isolated the section of html and css related t ...

Utilizing JQuery to send both files and text data via ajax请求

I am currently working on a Django web application that features a newsfeed functionality. Each newsfeed item includes a title, body, and image. Users can edit and update their newsfeeds, including adding new images. My current approach involves retrievin ...

The issue with Django's JavaScript functionality is that the dollar sign is not recognized and

Recently, I stumbled upon a fantastic chat feature in a Django project using version 2.0. The creator shared their work on Utopian here along with the git repository here. I tried to recreate the project following the given guide but faced challenges when ...

Guide on how to add multiple options to a select element in HTML using prototype.js

What is the best way to add multiple options to a select tag in HTML using prototype js? Appreciate your help. ...

php json with multiple dimensions

Unable to retrieve the deepest values from my json object, which contains an array of images listed as: { "imgs":[ { "Landscape":{ "2":"DSCF2719.jpg", "3":"DSCF2775.jpg", "4":"IMG_1586.jpg", ...

Continue iterating through the range of dates until the first date comes before the second date

I have a pair of Unix timestamps: let start: number = 1632988953; const end: number = 1638259353; My goal is to iterate over these two dates, recalculating the new start date in each iteration. To achieve this, I am utilizing a while loop as shown below ...

Utilize ajaxFileUpload.js to upload 3 images from various input type files in CodeIgniter effortlessly

I'm looking to enhance my code by enabling the upload of three images from different input types along with a text field using a single submit button function. The current code works fine when uploading only one file, but issues arise when attempting ...

Troubleshooting problem with Bootstrap 4 columns alignment - issue with nested columns wrapping

I attempted to create a column layout using Bootstrap 4, but unfortunately, the alignment is off. The nested columns do not display properly and the last one even breaks to a new line. <div class="row"> <div class="col-sm-3"> <d ...

How to extract the selected text within an input field using jQuery without relying on external plugins

Is it possible to retrieve the value of a selection made using jQuery's .select(); method? http://api.jquery.com/select/ I am looking for a way to obtain the selected characters within a text or input box without relying on any external plugins. ...

What is the best way to set a checkbox's default state in a react table using a value from Firestore?

Is there a way to initialize the checkboxes in React Table based on values from Firestore? I've tried reading the official documentation and looking at some examples, but I'm still confused about how to achieve this. My goal is to retrieve a val ...

Utilize AJAX to dynamically populate a list in CodeIgniter

Currently, I am working on a project that involves using Codeigniter. One of my tasks is to populate a dropdown list from the database using Ajax. However, I encountered an error: "Uncaught SyntaxError: missing ) after argument list." Below is the code sn ...

Socket.io transmitting [object Object] containing data from a mongoDB document

Happy Thanksgiving to all! I hope everyone enjoys the delicious food on their plates. I know I definitely will! Anyway, I'm currently attempting to query my mongoDB collection for the latest inserted document and send it to the client. However, inst ...

What is the most efficient way to combine several properties of the same element within an array?

I am working with an array containing personal information such as names and ages. My goal is to create a new property for each element in the array by combining certain properties together. Here is the initial array: var list = [{name: "James", ...

The dropdown menu with Bootstrap's <li> sub menu is not expanding as expected

<li class="dropdown"> <a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false">Utilitas<span class="c ...

Have Babel transpile configuration files into CommonJS format

I'm having trouble getting my Nuxt app to work with Jest for testing. I have a setupFilesAfterEnv property set with a setupTests.ts file that imports a translation file. The translations file uses ES6 import/export module syntax, but it seems like my ...

"Utilizing JavaScript, what is the method for obtaining unique values from dataTables and calculating the total of a specific field

How can I extract unique values from a DataTables dataset? The screenshot below illustrates the issue: https://i.sstatic.net/wUwOT.jpg In the example provided, the "Course 1" category contains duplicate values. My goal is to retrieve all distinct values ...