Achieving a dynamic "Picture Presentation" feature with JavaScript/jQuery

Currently, I am in the process of developing a website that will serve as a presentation. My plan is to create a slideshow effect using JavaScript. While I have implemented some functions, I must admit that it is not very organized at the moment. The main concept behind this project is to begin with the first slide, and upon pressing the space bar, the current slide will fade out while the next one fades in. Subsequently, pressing the space bar on the second slide should transition to the third, and so forth.

Below, you can find an example using JS BIN. However, I acknowledge that the implementation is far from ideal. Any assistance in refining and improving the functionality would be greatly appreciated. Ideally, I aim for the code to allow for easy addition of slides, but I am struggling to determine whether a for loop, switch statement, or another approach would best suit this purpose. Thank you for taking the time to read through this request.

JS BIN EXAMPLE

Answer №1

If you want to streamline your HTML and CSS code, you can avoid repetitive styling and excessive use of IDs for similar elements by following a few simple rules:

<div  class="section">Introduction</div>
  <div class="section">Chapter 1</div>
  <div class="section">Chapter 2</div>
  <div class='section'>
  Conclusion
  </div>

For the CSS part, keep it minimal and efficient:

body{
    font-family: 'apercuregular', arial, sans-serif;
    background-color: #96bff7;
    overflow:hidden;
    position: absolute;
    margin: 0px;
    top:     0px;
    left:    0px;
    width:  100%;
    height: 100%;
    z-index:  10;
}

.section{
    position:absolute;
    width:400px;
    top:170px;
    left:50%;
    margin-left:-26px;
    font-weight:300;
    line-height:110%;
    text-align:justify;
    background-color:#96bff7;
}

When it comes to the jQuery part, simplicity is key. Set z-index values, define slide numbers, and enable navigation with ENTER key press:

i = 0;
num_of_slides=4;

$('.section').each(function(i) {
    $(this).css('z-index',num_of_slides-i);
});

$(document).keypress(function(e) {
    if (e.keyCode == 13) {
        if (i <= num_of_slides) {
            i++;
            $('.section').eq(i).fadeIn(2000);
            $('.section').eq(i - 1).fadeOut(2000);

            if (i == num_of_slides) {
                i = 0;
                $('.section').eq(i).fadeIn(2000);
            }
        }
    }
});

Check out the live demo here: https://jsfiddle.net/tx0p4xge/

Answer №2

An option to explore is utilizing a bootstrap slideshow feature. For more information, check out this resource.

Answer №3

After playing around with your jsbin, I found that it worked well for me. However, I made a slight adjustment by changing the keycode to 13 (for Enter) instead of 32 (Spacebar), as I noticed this can vary between PC and Mac users. I also tweaked the css a bit - you can check out the updated code snippet below. I added some placeholder text; perhaps this could be an easier approach for you? Unless you already have prepared pages in place? In any case, I suggest making sure the content fits on the screen before presenting. Simply copy and paste the center divs and rename them center3, 4, etc. Your js currently loops through the pages, but you can see how to fix that - you accidentally killed the first slide! Remember, it's a work in progress.

A solid start to your presentation.

Rach

var lastLoaded = "";

$(document).ready(function(){
initIntro();
});

function initIntro(){
$("#title");

$(document).keypress(function(e){    

if (e.keyCode == 13) {           
killIntro();
}
});

}

function killIntro(){

$("#title").fadeOut(1000, function(){
$("body").remove("#title");
});

initPage0();
}


function initPage0(){
$("#center").fadeIn(1000, function(){
$("body");
});

$(document).keypress(function(e){    

if (e.keyCode == 13) {           
killPage();
}
});
}


function killPage(pg){
$("#center").fadeOut(1000, function(){
$("body").remove("#center");
initPage1();
});
}

function initPage1(){
$("#center2").fadeIn(1000, function(){
$("body").load("./pages/page1.php");
});
}
body{
font-family: 'apercuregular', arial, sans-serif;
background-color: #96bff7;
overflow:hidden;
position: absolute;
margin: 0px;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
z-index: 10;
}

h2, h3, h4{text-align:center;}

div section{text-align:justify;}
#title{
position:absolute;
width:100%;
height:100%;
top:5%;
margin:0 auto;
font-weight:300;
line-height:1.1em;
background-color:#96bff7;
z-index: 10;
}

#center{
position:relative;
width:100%;
height:100%;
top:5%;
margin:0 auto;
font-weight:300;
line-height:1.1em;
background-color:#96bff7;
z-index: 9;
}


#center2{
position:relative;
width:100%;
height:100%;
top:5%;
margin:0 auto;
font-weight:300;
line-height:1.1em;
background-color:#96bff7;
z-index: 8;
}
<!DOCTYPE html>
<html>
<head>
<link href="https://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Presentation</title>
</head>
<body>
<div id="title"><h2>Presentation</h2>
<h4>by Your Name</h4></div>
<div id="center"><h3>Page</h3>
<section><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce vitae scelerisque massa.......

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 transfer XML data format from a web browser to a server using jQuery?

Is there a method in jQuery to transmit data to the server from a browser in XML format? Thank you, Sana. ...

Load information from a JavaScript object when the user clicks dynamically

My challenge involves utilizing a JavaScript object that contains video information such as title and source URL. Within my HTML, I have image placeholders and the goal is to trigger a modal pop-up (using Twitter Bootstrap modal) of the specific video when ...

Issue with jquery selector function when handling ajax response containing HTML data

Within my jquery code, I have implemented an ajax get function to retrieve the html code of a specific page. My objective is to extract a particular element from this html content. However, when attempting to do so, jquery throws the following error: SCRI ...

Switch up the stylesheet in WordPress when an administrator is signed in

Looking for a way to incorporate conditional PHP code into my theme that will load a different stylesheet when either an admin or a specific user (ID=1) is logged in. Does WordPress have a function that can make this happen? There's a particular div ...

Is it possible for PHP to return jQuery code and have it execute immediately upon being echoed?

As someone who is new to jQuery and PHP, I have been tasked by my boss to create a login system for a web page that contains sensitive company information. The challenge is that this webpage consists of just one html/php file. Here is what I have done so ...

Disoriented InstancedMeshes causing confusion in THREE JS

Currently, I am experimenting with terrain generation code to generate water at a specific Y level and stone at another. This is just a preliminary model for my upcoming project on Minecraft terrain generation. However, I've encountered a problem wher ...

Pair of buttons triggering the submission of a form

I'm encountering an issue where I have 2 buttons in my form, one for submitting and one for going to the previous page, but both seem to be performing the same action. How is this happening? <div style="position:fixed; left: 50px; ...

Styling and theming in PrimeNG

Seeking advice on how to customize the appearance of the background for the primeNG panel component. I attempted to override the styling using the specific names in my scss file for the component, but it did not work. I also tried inline with <p-panel ...

What is the best way to retrieve the initial cell from a row that the user is currently hovering over?

Is there a way to extract the first cell from a row when a user hovers over it and then clicks a specific key combination? (considering using jQuery) I have a table set up similarly where placing the mouse over a tr and pressing ctrl+c will copy the ID t ...

Utilizing Google Maps in conjunction with anchor scrolling to a specific div element

Can someone help me with implementing a hover or click event on a google maps marker to scroll down a scrollable div? I've made some progress, but it's not working as expected: Check out the example here The scrolling behavior seems off and I c ...

Error: Unable to load Handlebars helper function

I am working on implementing a custom hbs helper in my Express.js application. However, I keep encountering an error message that says: Missing Helper "if_eq" The code for my page is as follows: <html> <head> <script src="javascript ...

Preventing Shift: How to Only Replace the Chosen Vue Draggable Item Without Affecting Other Grid Items

Let's try an example to demonstrate: https://codesandbox.io/s/j4vn761455?file=/src/App.vue:112-116 Step 1: Drag item 4 to where 5 is. Notice how 4 and 5 switch positions. Step 2: Now, drag item 1 to position 6. Watch as 1 moves to where 6 was, pushi ...

Whenever I use NextJS's <Link> component, I always end up getting redirected to a

After searching online, I came across this question and tried to implement the suggested solution, but it's still not working for me. Apologies for any duplication. I have a simple link tag that is resulting in a 404 error: <Link className={classe ...

code for handling window.location.href error

Continuing from the previous question, I am facing an issue with redirecting a form to an action while using Ajax. Despite my attempts to add a redirect in the Ajax function, I keep encountering errors and getting the wrong URL. The desired redirection is ...

Issue encountered during production build in Angular 6 due to NGRX

Trying to create and test the production build, I used the following command: ng build --prod --configuration=production ng serve --prod --configuration=production The build process completed successfully, however, upon opening the site on browsers, an ...

JavaScript to enable button functionality for selected items

Currently, I am developing a To-Do List application. Once users input information, it gets displayed in a table format. My objective is to have the ability to select specific items from this table and apply various button functionalities to them such as ma ...

Tips for evenly distributing these cards

Check out the full code on Codepen.io <div class="card-container"> <div class="card col-lg-4 col-sm-6 col-xs-12"> <div class="side">Jimmy Eat World</div> <div class="side back"><img ...

Exploring Angular Factory: Creating the getAll method for accessing RESTful APIs

In my Angular.JS factory, I am retrieving data from a REST Api. The REST Api endpoint is "/api/getSsls/1", where 1 represents the page number. The API response is in JSON format and contains the first ten items along with total pages/items information. I ...

Internet Explorer is unable to recognize the .less file format

I have created a less file that works perfectly in Chrome and Firefox, but it is not being recognized in IE 9. Does anyone have suggestions on how to make it run in IE 9.0? Below is a sample of the CSS code: @import "../core/variables.less"; @import "Mi ...

jQuery: Revealing or concealing several divs upon selection alteration

When I populate a form, I want to display or hide multiple divs based on the OPTION selected in a DROPDOWN. Currently, my code works but the issue is that one div can be hidden or shown by multiple OPTIONS. As a result, these divs keep toggling between hi ...