【问题标题】:How to make an image roll如何使图像滚动
【发布时间】:2015-03-02 16:47:25
【问题描述】:

我想制作一个类似xkcdsmbc 的next-prev 类型的网络漫画图片库

以下是我脑海中浮现的第一个想法。

HTML:

 <h1>Oscars 2015</h1>
 <button id="prev" onclick="prev(); return false;" disabled>PREVIOUS</button>
 <button id="next" onclick="next(); return false;">NEXT</button>
 <h3>(<span id="num">#</span>) <span id='title'>title</span></h3>
 <img id="comic"/>

JavaScript:

var img = [
 ["http://i.imgur.com/B1YclC5.jpg", "Birdman or (The Unexpected Virtue of Ignorance) won Best Picture."],
 ["http://i.imgur.com/XjAdinX.jpg", "Eddie Redmayne won Best Actor in a Leading Role for his role in The Theory of Everything."],
 ["http://i.imgur.com/kCj3uvC.jpg", "Julianne Moore won Best Actress in a Leading Role for her role in Still Alice."],
 ["http://i.imgur.com/OJq9hq1.jpg", "J K Simmons won Best Actor in a Supporting Role for his role in Whiplash."],
 ["http://i.imgur.com/qZ4XnIJ.jpg", "Patricia Arguette won Best Actress in a Supporting Role for her role in Boyhood."],
 ["http://i.imgur.com/QTGeTJ6.jpg?1","Big Hero 6 won Best Animated Feature Film."]
];

var i = 0;

function setComic() {
    console.log(img[i]);
    document.getElementById("comic").src = img[i][0];
    document.getElementById("num").textContent = i+1;
    document.getElementById("title").textContent = img[i][1];
}

setComic();

function prev() {
    i--;
    if (i <= 0) document.getElementById("prev").setAttribute('disabled', true);
    else document.getElementById("next").disabled = false;
    setComic();
    console.log(i);
}

function next() {
    i++;
    if (i >= img.length - 1) document.getElementById("next").disabled = true;
    else document.getElementById("prev").disabled = false;
    setComic();
    console.log(i);
}

在行动中看到它here

我想把按钮放在图片的上方和下方。所以,我做了,我用类替换了下一个/上一个 id,并使用了 document.getElementsByClass() 东西,但它没有像我想象的那样工作。 (code here)。而这只是冰山一角。

我的问题如下:

  1. 如何让我的代码更有效率?

  2. 对于慢速连接,如何更好地加载图像?

  3. 任何关于制作我想做的东西的好建议;资源、提示等。

【问题讨论】:

  • 我不知道你想问什么。请澄清您的问题,使其成为一个简单的问题陈述,并仅包含相关代码以重现您要解决的问题。
  • getElementsByClass() 给出一个元素数组,而不是单个元素。
  • @Nik,我很欣赏你的热情,但请尽量保持每个问题一个问题。你问的问题太宽泛了,无法回答。我认为此时您应该查看the StackOverflow help center article on how to ask a good question。如果您真的只是在进行代码审查,请在 Code Review 上发帖。
  • 只要且仅当代码按您的预期工作时,它就会成为 Code Review 的好帖子,以了解如何改进它。确保在问题中包含所有要审核的代码。
  • @nik - 是的,差不多。不过,您可以使用辅助函数来简化它。我将添加我可能接近它的方式 - 虽然只会使用幼稚的逻辑来启用/禁用按钮(您需要处理两种类型的按钮以响应对任一类型的点击 - 否则 prev 按钮将保持禁用状态,如果你到达终点,下一个按钮将被锁定在禁用状态)

标签: javascript html image storage


【解决方案1】:

这是一种方法。我已经添加了它,而不是简单地让问题通过代码审查,因为它无法正确启用/禁用按钮 - 您可以在上面看到我的评论以获取更多信息。

<!doctype html>
<html>
<head>
<script>
"use strict";
var img = [
    ["http://i.imgur.com/B1YclC5.jpg", "Birdman or (The Unexpected Virtue of Ignorance) won Best Picture."],
    ["http://i.imgur.com/XjAdinX.jpg", "Eddie Redmayne won Best Actor in a Leading Role for his role in The Theory of Everything."],
    ["http://i.imgur.com/kCj3uvC.jpg", "Julianne Moore won Best Actress in a Leading Role for her role in Still Alice."],
    ["http://i.imgur.com/OJq9hq1.jpg", "J K Simmons won Best Actor in a Supporting Role for his role in Whiplash."],
    ["http://i.imgur.com/qZ4XnIJ.jpg", "Patricia Arguette won Best Actress in a Supporting Role for her role in Boyhood."],
    ["http://i.imgur.com/QTGeTJ6.jpg?1", "Big Hero 6 won Best Animated Feature Film."],
    ["http://i.imgur.com/sYXnpMC.jpg", "Birdman or (The Unexpected Virtue of Ignorance) won Best Cinematography."],
    ["http://i.imgur.com/I0necik.jpg", "The Grand Budapest Hotel won Best Costume Design."],
    ["http://i.imgur.com/WKoIcqp.jpg", "Birdman or (The Unexpected Virtue of Ignorance) won Best Directing."],
    ["http://i.imgur.com/FAOxXO4.jpg", "CitizenFour won Best Documentary Feature."],
    ["http://i.imgur.com/7jWMv2d.jpg", "Crisis Hotline: Veterans Press 1 won Best Documentary Short Subject."],
    ["http://i.imgur.com/WMxECgv.jpg", "Whiplash won Best Film Editing"],
    ["http://i.imgur.com/igS4Vmh.jpg", "Ida won Best Foreign Language Film."],
    ["http://i.imgur.com/6PxTKRo.jpg", "The Grand Budapest Hotel won Best Makeup and Hairstyling."],
    ["http://i.imgur.com/H83ss3z.jpg", "The Grand Budapest Hotel won Best Original Score."],
    ["http://i.imgur.com/pD5LG2o.jpg", "Glory from the film Selma won Best Original Song."],
    ["http://i.imgur.com/7vJYuKI.jpg", "The Grand Budapest Hotel won Best Production Design."],
    ["http://i.imgur.com/Xpe3tVC.jpg", "Feast won Best Animated Short Film."],
    ["http://i.imgur.com/OIe1PGe.png", "American Sniper won Best Sound Editing."],
    ["http://i.imgur.com/PvqfxQe.jpg", "Whiplash won Best Sound Mixing."],
    ["http://i.imgur.com/2Z4jjz7.jpg", "Interstellar won Best Visual Effects."],
    ["http://i.imgur.com/AOciJZ2.jpg", "The Imitation Game won Best Adapted Screenplay."],
    ["http://i.imgur.com/jAWd8MK.jpg", "Birdman or (The Unexpected Virtue of Ignorance) won Best Original Screenplay."],
    ["http://i.imgur.com/sJM5sw2.jpg", "Cat tax."]
];

window.addEventListener('load', onDocLoaded, false);

function byId(elemId){return document.getElementById(elemId);}
function allByClass(className){return document.getElementsByClassName(className);}
function forEachNode(nodeList, func){for (var i=0, n=nodeList.length; i<n; i++) func(nodeList[i], i, nodeList); }

function onDocLoaded(evt)
{
    var prevBtns = allByClass('prevBtn');
    var nextBtns = allByClass('nextBtn');
    forEachNode(prevBtns, function(elem, index, list) { elem.addEventListener('click', onPrevBtnClick, false); } );
    forEachNode(nextBtns, function(elem, index, list) { elem.addEventListener('click', onNextBtnClick, false); } );
    setComic();
}


var curImgIndex = 0;

function setComic()
{
    console.log(img[curImgIndex]);
    document.getElementById("comic").src = img[curImgIndex][0];
    document.getElementById("num").textContent = curImgIndex + 1;
    document.getElementById("title").textContent = img[curImgIndex][1];
}

function enableButtons(className, isEnabled)
{
    var btnList = allByClass(className);
    forEachNode(btnList, nodeFunc);

    function nodeFunc(elem, index, list)
    {
        elem.disabled = !isEnabled;
    }
}

function onPrevBtnClick()
{
    --curImgIndex;
    if (curImgIndex < 1)
        enableButtons('prevBtn', false);
    else 
        enableButtons('prevBtn', true);

    enableButtons('nextBtn', true);
    setComic();
    console.log(curImgIndex);
}

function onNextBtnClick() 
{
    ++curImgIndex;
    if (curImgIndex > img.length - 2)
        enableButtons('nextBtn', false);
    else 
        enableButtons('nextBtn', true);

    enableButtons('prevBtn', true);
    setComic();
    console.log(curImgIndex);
}
</script>
<style>
img {
    height:250px;
}
</style>
</head>
<body>
    <h1>Oscars 2015</h1>

    <!--Top Controls-->
    <button class="prevBtn" disabled>PREVIOUS</button>
    <button class="nextBtn">NEXT</button>

    <h3>(<span id="num">#</span>) <span id='title'>title</span></h3>

    <img id="comic" />
    <br/>
    <br/>
    <!--Bottom Controls-->
    <button class="prevBtn" disabled>PREVIOUS</button>
    <button class="nextBtn">NEXT</button>
</body>
</html>

【讨论】:

  • 这就是我需要的答案!非常非常非常感谢你!! :D 天哪,这很漂亮:)
  • @Nik - 不客气。我们都站在前人的肩膀上。希望有一天我能过去,自己借一杯糖。 :p
【解决方案2】:

您可以为此使用 css,并将您的 html/jquery 保持在最低限度。

注意我是如何让 jquery 保持简单,并从左侧的 img 标记中提取 src 属性,然后将其用作我的 .big 图像的背景?这样,我通过使用已经存在的图像将请求保持在最低限度

$('.sidepanel img').click(function() {
  var val = $(this).attr("src");
  $('.gallery .big').css("background", "url(" + val + ")").css("background-size", "100% 100%");
});
.sidepanel {
  width: 100px;
  height: 400px;
  display: inline-block;
}
.sidepanel img {
  height: 100px;
  width: 100px;
}
.gallery .big {
  height: 400px;
  width: 400px;
  background: lightgray;
  background-size: 100% 100%;
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="gallery">
  <div class="sidepanel">
    <img src="http://placekitten.com/g/300/300" />
    <img src="http://placekitten.com/g/200/300" />
    <img src="http://placekitten.com/g/300/200" />
    <img src="http://placekitten.com/g/300/150" />
  </div>
  <div class="big"></div>
</div>

【讨论】:

  • 不完全是我想要的。您从未在线阅读过漫画吗?我期待着那种形式的东西。这是一个漂亮干净的小画廊,但仍然不是我所希望的。
  • @Nik:由于我的编程量很大,很遗憾我没有时间阅读漫画。
  • :D 呵呵,我描述的表单只是一个下一个/上一个按钮图片库,后面有一些我不完全了解的 cms。
  • @Nik:因此您的问题没有得到很多答案。我并不是说一定是为什么,但这是一个很大的障碍。但这就是我的工作方式。 (保持简单)。您总是可以“隐藏”侧边栏,并通过按钮循环遍历子元素?
  • :D 这实际上是一个非常巧妙的解决方案。我会考虑的。谢谢:D
【解决方案3】:

没有理由让每个图像都有自己的数组,例如,您可以只使用split(', ') 功能。

另外,我添加了一个接收参数的“更改图像”功能,因此您最终使用的代码更少。

var images = [
    'http://i.imgur.com/B1YclC5.jpg, description0, alt0',
    'http://i.imgur.com/XjAdinX.jpg, description1, alt1',
    'http://i.imgur.com/kCj3uvC.jpg, description2, alt2',
    'http://i.imgur.com/OJq9hq1.jpg, description3, alt3'],
    max = parseInt(images.length - 1),
    curr = 0;

function display(a, b, c) {
    img = document.getElementById('myImage');
    img.src = a;
    img.setAttribute('alt', b);
    document.getElementById('desc').innerHTML = c;
}

function next() {
    if (curr < max) {
        curr += 1;
        arr = images[curr].split(', ');
        path = arr[0];
        desc = arr[2];
        alt = arr[1];
        display(path, desc, alt);
    }
}

function prev() {
    if (curr > 0) {
        curr -= 1;
        arr = images[curr].split(', ');
        path = arr[0];
        desc = arr[2];
        alt = arr[1];
        display(path, desc, alt);
    }
}

http://jsfiddle.net/scott88/p4utrd7v/

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多