js to realize the rotation of web page pictures

js to realize the rotation of web page pictures

This article shares the specific code of js to realize the rotation of web page pictures for your reference. The specific content is as follows

1. The effects are as follows:

2. Implement functions:

(1) After clicking the left or right arrow, the picture displayed below will be replaced by the corresponding previous or next picture.

(2) When you click on a picture in the navigation, the corresponding picture below will be displayed, and when you click on the previous or next picture again, the corresponding picture will be displayed

(3) The image address can come from the Internet or be a string array sent by your own server.

3. Implementation code:

(1) Directory structure:

(2) The code content of index.html is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Image Rotation</title>
 <script type="text/javascript" src="js/showPic.js"></script>
 <link rel="stylesheet" type="text/css" href="css/mystyle.css"/>
</head>
<body>
    <img id="picture" src="image/1.jpg"alt="my image"/>
    <div id="navigate">
    <ul id="image">
          <li>
              <a href="#" title="Left Arrow" οnclick="clickTurnLeft();">
                 <img src="image/left_aim.jpg" id="leftAim">
              </a>
          </li>
   <li>
             <a href="image/1.jpg" title="Flowers" οnclick="showPic(this);return false;">
          <img src="image/1.jpg" id="smallPic" alt="Flower thumbnail">
             </a>
   </li>
   <li>
             <a href="image/2.jpg" title="White Snow" click="showPic(this);return false;">
                <img src="image/2.jpg" id="smallPic"alt="snow thumbnail">
             </a>
   </li>
   <li>
      <a href="image/3.jpg" title="Flying Bird" οnclick="showPic(this);return false;">
                <img src="image/3.jpg" id="smallPic"alt="Bird thumbnail">
             </a>
   </li>
   <li>
       <a href="image/4.jpg" title="Rock" click="showPic(this);return false;">
          <img src="image/4.jpg" id="smallPic"alt="stone thumbnail">
              </a>
   </li>
          <li>
             <a href="#" title="Right Arrow" οnclick="clickTurnRight();">
                <img src="image/right_aim.jpg" id="rightAim"alt="Rotate right">
             </a>
         </li> 
      </ul>
   </div>   
</body>
</html>

(3) The code content of mystyle.css is as follows:

/* mystyle.css code */
 
body {
 text-align:center
}
#navigate{
 margin:0 auto; 
 width:1100px; 
 height:100px;
}
ul{
 margin-right:auto;margin-left:auto;
}
li{
 float:left;
 padding:10px;
 list-style:none;
}
 
#leftAim{
 width:100px;
 height:100px;
}
#smallPic{
 width:180px;
 height:120px;
 border:2px solid black;
}
#rightAim{
 width:100px;
 height:100px;
}
#picture{
 display:block;
 width:800px;
 height:600px;
 margin:0 auto;
}

(4) The code content of showPic.js is as follows:

//showPic.js
var href = new Array("image/1.jpg","image/2.jpg","image/3.jpg","image/4.jpg"); 
var index = 0 ; 
 
function clickTurnLeft() {
 if (index == 0) {
  index = href.length - 1; 
 } else {
  index = --index % href.length ; 
 }
    var picture = document.getElementById("picture");
   picture.setAttribute("src",href[index]);
}
 
function clickTurnRight(){
 index = ++index % href.length; 
 var picture = document.getElementById("picture");
   picture.setAttribute("src",href[index]);
}
 
function showPic(whichPic){
 var source = whichPic.getAttribute("href");
 index = href.indexOf(source);
  var picture = document.getElementById("picture");
  picture.setAttribute("src",source);
}

4. Summary:

An array of image names is defined in the JS file. This array can be the image address data returned by the server or the image address on the network.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Automatic information rotation playback in js drop-down menu
  • js Sina's picture playback picture rotation effect code

<<:  Detailed explanation of Linux inotify real-time backup implementation method

>>:  Mysql index types and basic usage examples

Recommend

Vue parent-child component mutual value transfer and call

Table of contents 1. Parent passes value to child...

Detailed explanation of ActiveMQ deployment method in Linux environment

This article describes the deployment method of A...

Detailed explanation of important cascading concepts in CSS

Recently, I encountered a problem in the process ...

Detailed explanation of Svn one-click installation shell script under linxu

#!/bin/bash #Download SVN yum -y install subversi...

Detailed explanation of table return and index coverage examples in MySQL

Table of contents Index Type Index structure Nonc...

MySQL data backup and restore sample code

1. Data backup 1. Use mysqldump command to back u...

Detailed explanation of as, question mark and exclamation mark in Typescript

1. The as keyword indicates an assertion In Types...

Create a custom system tray indicator for your tasks on Linux

System tray icons are still a magical feature tod...

CentOS8 installation tutorial of jdk8 / java8 (recommended)

Preface At first, I wanted to use wget to downloa...

9 Practical CSS Properties Web Front-end Developers Must Know

1. Rounded Corners Today's web designs are con...

Detailed explanation of Truncate usage in MYSQL

This article guide: There are two ways to delete ...

The difference between MySQL user management and PostgreSQL user management

1. MySQL User Management [Example 1.1] Log in to ...

Detailed explanation of the problem of CSS class names

The following CSS class names starting with a num...

SVN installation and basic operation (graphic tutorial)

Table of contents 1. What is SVN 2. Svn server an...