简体   繁体   中英

Changing pictures in a website gallery form

I have tried for about a week now and cannot solve my problem. What I want to do is have a web page that will basicaly scroll through 5 or 6 pictures by clicking a next or prev asp:button . Can someone help me from scratch how to achieve this.

I am using visual studio web developer and have a blank web site made with a master page. I want to add this gallary function to the auto generated Default.apsx file.

My picture name are pic001.jpg; pic002.jpg; pic003.jpg pic001.jpg; pic002.jpg; pic003.jpg pic001.jpg; pic002.jpg; pic003.jpg and so forth

all I want is a button on the left saying previous which will take you to the previous image, a button on the right that says next which will show you the next picture and in the middle is a picture (the one that changes)

Please help with this problem I have tried and failed rather horribly at it, Many thanks to the one who can help

This is some code of my attempts:

Default.aspx file

<%@ Page Title="Default" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default"  %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server" >
    <script src="myJava.js" type="text/javascript"></script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server" >
<table>
    <tr><td> GALLARY </td></tr> <!--Header-->
    <tr>
        <td> <asp:Button ID="Button1" runat="server" Text="Prev" OnClientClick="getPrevImage()"/> </td>
        <td> <img ID="pic" alt="" src="" runat="server" width="400" height="400" /> </td>
        <td> <asp:Button ID="Button2" runat="server" Text="Next" OnClientClick="getNextImage()"/> </td>
    </tr>
</table>
</asp:Content>

Default.aspx.cs file

public partial class Default : System.Web.UI.Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string script = string.Empty;
            script += "<script language='javascript'>";

            script += "init()";
            script += "</script>";
            ClientScript.RegisterClientScriptBlock(this.GetType(), "Error", script);
        }
    }
}

myJava.js file

var imagePath = new Array();
var imageIndex = 0;

function init(){
     addPath("pic001.jpg");
     addPath("pic002.jpg");
     addPath("pic003.jpg");
     addPath("pic004.jpg");
     addPath("pic005.jpg");

     getImagePath(0);
}

function addPath(path){
     var index = imagePath.length;
     imagePath[index++] = path;  
}

function getImagePath(index){
     var length = imagePath.length;

     if(index <= length){
        if(index >= 0){
            document.getElementById("MainContent_pic").src = imagePath[index];
            document.getElementById("MainContent_pic").alt = imagePath[index];
            imageIndex = index;
        }
     } else {
        document.getElementById("MainContent_pic").src = "DOES NOT EXIST";
        document.getElementById("MainContent_pic").alt = "DOES NOT EXIST";
     }
}

function getNextImage(){
     var length = imagePath.length;
     var index = imageIndex;
     if(index++ < length--){
        if(imagePath[index] != null){
            imageIndex = index;
            document.getElementById("MainContent_pic").src = imagePath[index];
            document.getElementById("MainContent_pic").alt = imagePath[index];
        }                                    
     }
}

function getPrevImage(){
     var index = imageIndex;
     if(index-- >= 0){
        if(imagePath[index] != null){
            imageIndex = index;
            document.getElementById("MainContent_pic").src = imagePath[index];
            document.getElementById("MainContent_pic").alt = imagePath[index];
        }   
     }
}

Here are an approaching using html and jquery that you can easy use in your app:

HTMl (feel free to replace <a> with any tag that you want):

<a href="" class="previous">Previous</a>

<img src="1.jpg" class="display"/>
<img src="2.jpg" class="hide"/>
<img src="3.jpg" class="hide"/>
<img src="4.jpg" class="hide"/>

<a href="" class="next">Next</a>

Jquery:

$(document).ready(function(){
    $(.previous).click(function(){
        var current = $(.display);
        //hide current img
        current.attr("class", "hide");
        //get previous img
        var pre = current.pre();
        if(pre !== null)
        {
            //display previous img if available
            pre.atrr("class", "display");
        }
        else
        {
            //dipslay last img if previous not available
            current.parent().find("img:last").attr("class", "display");
        }
    });

    $(.next).click(function(){
        var current = $(.display);
        //hide current img
        current.attr("class", "hide");
        //get next img
        var next = current.next();
        if(next !== null)
        {
            //display next img if available
            next.atrr("class", "display");
        }
        else
        {
            //display first img if next not available
            current.parent().find("img:first").attr("class", "display");
        }
    })
});

CSS (you can change the display: block to inline or anything that suit your requirement):

img.display {
    display: block;
}

img.hide {
    display: none;
}

I'm not a pro html, css, jquery so hope this somehow help you

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM