Having trouble with your jQuery image slider?

As a newcomer to jQuery, I have been experimenting with it to gain some experience. I recently tried to integrate a jQuery image slider from slidesjs.com into my local website but unfortunately, it is not functioning as expected.

Additionally, I would like to organize my code better. How can I link my jQuery page with the HTML if I decide to move all the jQuery code into a separate file?

http://jsfiddle.net/3WaWf/1/

Here is an example of what I have attempted:

<html>
    <header>
      <script src="http://code.jquery.com/jquery-latest.min.js"></script>
      <script src="jquery.slides.min.js"></script>
      <link rel="stylesheet" type="text/css" href="style2.css"></header>

        <style>
        /* Prevents slides from flashing */
        #slides {
          display:none;
        }
      </style>

      <script>
        $(function(){
          $("#slides").slidesjs({
            width: 940,
            height: 528
          });
        });
      </script>

    <body>
        <div class="nav-bar">
            <h1 class="logo" style="vertical-align:middle">LogoHere</h1>

            <ul class="nav-menu">
                <li><a href="index.html" class="action">Action</a></li>
                <li><a href="about.html" class="about">Who we are</a></li>
                <li><a href="blog.html" class="blog">Blog</a></li>
                <li><a href="services.html" class="services">Services</a></li>
                <li><a href="contact.html" class="contact">Get in touch</a></li>
            </ul>

            <div id="slides">
                <img src="hkslide1.jpg">
                <img src="hkslide2.jpeg">
                <img src="hkslide3.jpg">
            </div>
        </div>

Answer №1

Make sure to place your js and css include sections correctly, as well as verifying the paths for any errors. If they are not found, you may encounter 404 errors in your browser console.

Here is an example of code:

<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="jquery.slides.min.js"></script>
    <link rel="stylesheet" type="text/css" href="style2.css">
</head>

If the demo code is set up properly, it should work without any issues.

Demo: http://jsfiddle.net/IrvinDominin/tzPE2/

Answer №2

To ensure that jquery.slides.min.js is in the same directory as the main page, and to organize the JQuery code effectively, simply place it in a separate .js file and reference it on the page using this syntax: instead of embedding the code directly.

Answer №3

It is important to place these elements between the <head> and </head> tags, not within the <header>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="jquery.slides.min.js"></script>
<link rel="stylesheet" type="text/css" href="style2.css">

Edit

<!doctype html>

<html>

<head>

  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script src="jquery.slides.min.js"></script>
  <link rel="stylesheet" href="style2.css" media="screen">

  <style>
    /* Prevents slides from flashing */
    #slides {
      display:none;
    }
  </style>

</head>

<body>

    <div class="nav-bar">

      <h1 class="logo" style="vertical-align:middle">LogoHere</h1>

      <ul class="nav-menu">
          <li><a href="index.html" class="action">Action</a></li>
          <li><a href="about.html" class="about">Who we are</a></li>
          <li><a href="blog.html" class="blog">Blog</a></li>
          <li><a href="services.html" class="services">Services</a></li>
          <li><a href="contact.html" class="contact">Get in touch</a></li>
      </ul>

      <div id="slides">
          <img src="hkslide1.jpg">
          <img src="hkslide2.jpeg">
          <img src="hkslide3.jpg">
      </div>

    </div>

<!-- SCRIPTS -- If you're putting scripts in HTML, put them below this line -->

  <script>
    $(function(){
      $("#slides").slidesjs({
        width: 940,
        height: 528
      });
    });
  </script>

</body>

</html>

In this case, remember that the files jquery.slides.min.js and style2.css should be located in the root folder, same place as your index.html

Answer №4

If you're encountering issues with your code not executing properly, consider delaying its execution until after the page elements have finished loading. One potential solution is to move your JavaScript code to the footer while keeping the stylesheet in the head section. Give this approach a test to see if it resolves the issue.

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 navigate back to the top of the page once a link has been clicked?

One issue I'm facing is that whenever I click on a link in NextJS, it directs me to the middle of the page: <Link href={`/products/${id}`} key={id}> <a> {/* other components */} </a> </Link> I believe the problem l ...

Tips on applying the "activate" class to a Bootstrap navbar in Angular 2 when implementing page anchor navigation

As I work on my single-page website using Angular 2 and Bootstrap 4, I have successfully implemented a fixed navbar component that remains at the top of the page. Utilizing anchor navigation (#id), the navbar allows smooth scrolling to different sections o ...

What is the best way to extract a particular key value from a JSON object?

I am completely new to the world of APIs and just starting out with JavaScript. My goal is to retrieve the status of a server from a server hosting panel using an API. In order to achieve this, I need to log in by making a request to /API/Core/Login, extra ...

Encountering a problem involving the apostrophe character "'" when trying to save content into a MySQL database

I have been developing an application that allows users to create HTML templates and save them. Users can utilize different components such as text, images, etc. to build HTML pages. Challenge: The issue I'm encountering is when a user inputs text wi ...

Implementing CSS and JS for targeted elements or functions in WordPress

I am currently managing a WordPress WooCommerce website My goal is to implement bootstrap CSS and js specifically for a function on my WooCommerce checkout page. The code I have written is as follows: if( current_user_can('administrator')) { fu ...

Can you explain the variances between the index.html and index-async.html files within the AngularJS seed project?

Within the angularjs seed project, there are two files that appear to produce identical results: index.html and index-async.html. If you want to view the code for index-async.html, you can visit this link. Similarly, if you're interested in the code ...

Is it possible to call a Node.js function from JavaScript using a <script> tag in a Jade template?

I have been working on developing a blog using Node.js, Express, and MongoDB. In the template for creating a new post, there are fields for both a title and a slug. To generate slugs, I am utilizing the Slugs for Node.js library from NPM: https://npmjs.or ...

Executing npm / http-server script

I'm currently working on a shell script that will compile an Angular app using the "ng build" command and then launch a web server to host the app from the dist folder. The web server will be started with the "http-server" command, which needs to be i ...

Navigating data within a JSON file with D3 javascript: a step-by-step guide

Currently, I am attempting to extract and manipulate data from a JSON file using D3 Javascript. Below is the content of the JSON file: { "Resources": [ { "subject": "Node 1", "group" : "1" }, { "predicate": ...

What is the process for building an interactive quiz using JavaScript?

In the process of creating a quiz, I envision a user interface that presents questions in a "card" format. These questions will include simple yes/no inquiries and some multiple-choice options. As the user progresses through the quiz, their answers will de ...

Minimize the gap between legend text and icon in Highchart

Is there a way to decrease the space between the number and icon? I am currently working with Angular 8 and Highchart. Below is the configuration of the chart legend. https://i.stack.imgur.com/0dL7y.jpg this.legend = { align: 'center', verti ...

What causes the behavior of Node.js to be the way it is?

Check out this code snippet function removePrototype() { var obj = {}; for (var _i = 0, _a = Object.getOwnPropertyNames(obj.__proto__); _i < _a.length; _i++) { var prop = _a[_i]; obj[prop] = undefined; } obj.__proto__ = ...

Ways to customize the appearance of Angular material elements one by one?

I have a component that contains two Angular Material form components: <!-- First input --> <mat-form-field> <mat-label>Password</mat-label> <input matInput type="text"> </mat-form-field> <br&g ...

A function injected into a constructor of a class causes an undefined error

As I delve into learning about utilizing typescript for constructing API's, I have encountered a couple of challenges at the moment. Initially, I have developed a fairly straightforward PostController Class that has the ability to accept a use-case wh ...

Tips for properly formatting large numbers

I am facing a challenge with handling large numbers in my JavaScript code. I came across the nFormatter function for formatting large numbers but I am unsure how to integrate it into my existing code. Below is the nFormatter function that I found: functi ...

What is the rationale behind not passing $scope to a service in AngularJS, and is it considered bad practice?

Is it advisable not to pass $scope to a service for certain reasons? I understand that services are intended to be reusable singletons, and passing a (potentially) large object to the service could lead to maintenance issues. However, assuming there is so ...

A step-by-step guide to incorporating expandable and collapsible images within a div element using X

I have successfully created dynamic divs with some data that expand and collapse perfectly. Now I am looking to add expand and collapse images on these divs. I am relatively new to designing in xslt. <xsl:template match="category[key!='org.model.C ...

How can I position the close button correctly in a bootstrap popup modal?

I have a basic bootstrap popup modal. Currently, the close X button is centered within the popup window, but I would like to align it to the right corner instead. Is there a way to achieve this? <div class="modal fade" id="checkNameSurvey-modal" data ...

Problem with vueJS List Transition not being triggered

Within my Vue JS App, I encountered a situation where I have a list of items that change order randomly when the user clicks a button. Despite successfully using Vue.set to dynamically reposition the list elements, I faced an issue with adding a transition ...

What methods can be used to customize the font and background color of a website for different user groups?

Trying to incorporate a template into my project. My client has requested the following: The regular user area should feature a blue background. The professional user area should have an orange background. Is there a way to set up a condition to change ...