将键盘控件插入图像查看器库脚本

时间:2014-07-19 01:33:15

标签: javascript php

我想将键盘控制左箭头键和右箭头键添加到脚本中:

  

http://xenforo.com/community/threads/php-file-that-basically-functions-as-folder-image-viewer.14658/

左箭头=后退,右箭头=下一步。

每次我把功能放在前面?>它总是返回错误。

我使用这种方法

  

jQuery keypress left/right navigation

$("document").keypress(function (e) { //always error here
  if(e.keyCode == 37) { // left
    //back (what here next i don't know too)
  }
  else if(e.keyCode == 39) { // right
    //next
  }
});

这里是完整的脚本

<?php
// ------------------------------------------------------------------------- //
// Comic Gallery 1.2                                                        //
// ------------------------------------------------------------------------- //
// Copyright (C) 2005 Stuart Robertson                                      //
// http://www.designmeme.com/                                                //
// ------------------------------------------------------------------------- //
// This program is free software; you can redistribute it and/or modify      //
// it under the terms of the GNU General Public License as published by      //
// the Free Software Foundation; either version 2 of the License, or        //
// (at your option) any later version.                                      //
//  A summary is available at http://creativecommons.org/licenses/GPL/2.0/  //
// ------------------------------------------------------------------------- //
// Edit the code below to configure your Comic Gallery                      //
// ------------------------------------------------------------------------- //

// Your images directory, relative to the page calling this script
$imagedir="comics";

// To start at the last image use "last"
$startimage="first";

// Copyright name to display, for none use " "
$copyright=" ";

// Creative Commons license, for none use " "
// example: "http://creativecommons.org/licenses/by/2.0/"
$creativecommons=" ";

// type of divider, for none use " "
$divider="&middot;";

// show arrows, for none use 0
$arrows=1;

// show back and next, for none use 0
$backnext=1;

// show back and next, for none use 0
$firstlast=1;

// show numbers, for none use 0
$numbers=1;

// numbers per line
$linelength=10;

// navigation position, for aboe use "above"
$navplacement="below";

// ------------------------------------------------------------------------- //
// Do not edit below this line
// ------------------------------------------------------------------------- //

//    initialize variables
$pics=array();
$count=0;

// Open the directory
$comicdir=opendir($imagedir);

//    read directory into pics array
while (($file = readdir($comicdir))!==false) {
    //    filter for jpg, gif or png files...
    if (substr($file,-4) == ".jpg" || substr($file,-4) == ".gif" || substr($file,-4) == ".png" || substr($file,-4) == ".JPG" || substr($file,-4) == ".GIF" || substr($file,-4) == ".PNG"){
        $pics[$count] = $file;
        $count++;
        }
}
closedir($comicdir);

// check for the picture to view
$pic=$_GET['p'];
//    if no picture variable...
if ($pic=="") {
    if ($startimage!="last"){ $pic=1; }
    else { $pic=$count; }
}

//    sort the filenames alphabetically
sort($pics);
reset($pics);

//    determine which picture to get
for ($f=0;$f<=sizeof($pics)-1;$f++){if ($pic==$pics[$f]){$selected = $f+1;}}

//  check for javascript...
if ($pic && !preg_match("/javascript/",$pic)){

    //  get current image file
    $current=$pics[$pic-1];
    $next=$pic+1;
    if ($next > sizeof($pics)){ $next=sizeof($pics); }
    $back=$pic-1;
    if ($back < 1){ $back=1; }

    //  display image above nav
    if ($navplacement!="above"){
        if (substr($current,-4) == ".jpg" || substr($current,-4) == ".gif" || substr($current,-4) == ".png" || substr($current,-4) == ".JPG" || substr($current,-4) == ".GIF" || substr($current,-4) == ".PNG"){
                if ($pic < sizeof($pics)){
                    echo"\n<p id=\"cg_img\"><a href=\"?p=".$next."\"><img src=\"".$imagedir."/".$current."\" alt=\"Next\" /></a></p>\n";
                } else {
                echo"\n<p id=\"cg_img\"><img src=\"".$imagedir."/".$current."\" alt=\"End\" /></p>\n";
                }
            }
    }

    // display back and next
    if ($backnext != 0 || $arrows != 0){
            if (sizeof($pics) > 1){
                echo "<p id=\"cg_nav1\">";
                if ($firstlast != 0){
                    if ($pic > 1){    echo "<a href=\"?p=1\" id=\"cg_first\"><span>First</span></a>"; }
                    else { echo "<span id=\"cg_first\"><span>First</span></span>"; }
                    echo "<span class=\"cg_divider\"> ".$divider." </span>";
                }
                if ($pic > 1){
                    echo "<a href=\"?p=".$back."\" id=\"cg_back\"><span>";
                    if ($arrows != 0) { echo "&laquo; "; }
                    if ($backnext != 0) { echo "Back"; }
                    echo "</span></a>";
                } else {
                    echo "<span id=\"cg_back\"><span>";
                    if ($arrows != 0) { echo "&laquo; "; }
                    if ($backnext != 0) { echo "Back"; }
                    echo "</span></span>";
                }
                echo "<span class=\"cg_divider\"> ".$divider." </span>";
                if ($pic < sizeof($pics)){
                    echo "<a href=\"?p=".$next."\" id=\"cg_next\"><span>";
                    if ($backnext != 0) { echo "Next"; }
                    if ($arrows != 0) { echo " &raquo;"; }
                    echo "</span></a>";
                } else {
                    echo "<span id=\"cg_next\"><span>";
                    if ($backnext != 0) { echo "Next"; }
                    if ($arrows != 0) { echo " &raquo;"; }
                    echo "</span></span>";
                }
                if ($firstlast != 0){
                    echo "<span class=\"cg_divider\"> ".$divider." </span>";
                    if ($pic < sizeof($pics)){    echo "<a href=\"?p=". sizeof($pics) ."\" id=\"cg_last\"><span>Last</span></a>"; }
                    else { echo "<span id=\"cg_last\"><span>Last</span></span>"; }
                }
                echo "</p>\n";
            }
    }

    // display numbers
    if ($numbers != 0){
        if (sizeof($pics) > 1){
            //    display textlinks
            echo "<p id=\"cg_nav2\">";
            // loop over images
            for ($f=1;$f<=sizeof($pics);$f++){
                // if the link to the pic is the selected one, display a bold number and no link
                if ($pic==$f){echo "<b>".$f."</b>";}
                // otherwise display the link
                else{echo "<a href=\"?p=".$f."\">".$f."</a>";}
                // add dividers and linebreaks
                if (($f % $linelength) == 0) {
                    echo "<br />";
                }
                else {
                    if ($f!=sizeof($pics)){
                        echo "<span class=\"cg_divider\"> ".$divider." </span>";
                    }
                }
            }
            echo "</p>\n";
        }
    }

    //  display image below nav
    if ($navplacement=="above"){
        if (substr($current,-4) == ".jpg" || substr($current,-4) == ".gif" || substr($current,-4) == ".png" || substr($current,-4) == ".JPG" || substr($current,-4) == ".GIF" || substr($current,-4) == ".PNG"){
                if ($pic < sizeof($pics)){
                    echo"\n<p id=\"cg_img\"><a href=\"?p=".$next."\"><image src=\"".$imagedir."/".$current."\" alt=\"Next\" border=\"0\"></a></p>\n";
                } else {
                echo"\n<p id=\"cg_img\"><image src=\"".$imagedir."/".$current."\" alt=\"End\" /></p>\n";
                }
            }
    }

    // display copyright
    echo "<p id=\"cg_credits\">";
    if ($creativecommons != " "){ echo "<a href=\"".$creativecommons."\" title=\"Creative Commons License\">Some Rights Reserved</a> ".$divider." "; }
    else {
        if ($copyright != " "){ echo "&copy; ".$copyright." ".$divider." "; }
    }
    // If you make use of this script, be nice and keep the link back to my site :-)
    echo "Powered by <a href=\"http://designmeme.blogspot.com/2005/05/comic-gallery-script.html/\">ComicGallery</a></p>\n";
}
?>

2 个答案:

答案 0 :(得分:1)

PHPJavaScript是两种独立的编程语言 - 您不能将它们混合在一起。当您将jQuery方法直接放在PHP代码中时(例如,在'?&gt;'之前,您告诉PHP运行该代码 - 但PHP不知道如何,因为它使用的是另一种语言。

相反,请尝试将方法置于PHP的标记之外(<? ... ?>)或echo,具体取决于页面其余部分的设置方式。


欢迎来到Stack Overflow!

答案 1 :(得分:0)

好的,谢谢@tbd,echo这个方法很精彩。 像魅力一样工作

echo "<script type=\"text/javascript\">" ;
echo "function keyNav( e )"; echo "{ ";
echo "if( !e )"; echo"{ e = window.event; }" ;

echo "goBack = 37;" ;
echo "goNext = 39;" ;

echo "switch( e.keyCode )";
echo "{";
echo "case goBack: window.location.href='?p=".$back."\ '
break;";
echo "case goNext: window.location.href='?p=".$next."\ '
break;";
echo "}";
echo "}";

echo "document.onkeydown = keyNav;";
echo "</script>";

太多echo我不知道是否有更简单的方法,但这个对我来说已经足够了,谢谢。