【问题标题】:Links Are Showing Of Next Slide In A Slider. Why?链接在滑块中显示下一张幻灯片。为什么?
【发布时间】:2014-11-14 07:33:23
【问题描述】:

我正在制作一个滑块,一切正常,但问题是它在当前幻灯片的下一张幻灯片上显示链接。如何解决?您可以通过 http://jsfiddle.net/gwbqvqjs/

观看现场演示
<script type='text/javascript'>
//<![CDATA[    
/* general variables */
var galleryId = 'gallery'; /* change this to the ID of the gallery list */
var gallery; /* this will be the object reference to the list later on */
var galleryImages; /* array that will hold all child elements of the list */
var currentImage; /* keeps track of which image should currently be showing */
var previousImage;
var preInitTimer;
preInit();
/* functions */
function preInit() {
    /* an inspired kludge that - in most cases - manages to initially hide the image gallery list
       before even onload is triggered (at which point it's normally too late, and the whole list already
       appeared to the user before being remolded) */
    if ((document.getElementById)&&(gallery=document.getElementById(galleryId))) {
        gallery.style.visibility = "hidden";
        if (typeof preInitTimer != 'undefined') clearTimeout(preInitTimer); /* thanks to Steve Clay http://mrclay.org/ for this small Opera fix */
    } else {
        preInitTimer = setTimeout("preInit()",2);
    }
}
function fader(imageNumber,opacity) {
    /* helper function to deal specifically with images and the cross-browser differences in opacity handling */
    var obj=galleryImages[imageNumber];
    if (obj.style) {
        if (obj.style.MozOpacity!=null) {  
            /* Mozilla's pre-CSS3 proprietary rule */
            obj.style.MozOpacity = (opacity/100) - 0;
        } else if (obj.style.opacity!=null) {
            /* CSS3 compatible */
            obj.style.opacity = (opacity/100) - 0;
        } else if (obj.style.filter!=null) {
            /* IE's proprietary filter */
            obj.style.filter = "alpha(opacity="+opacity+")";
        }
    }
}
function fadeInit() {
    if (document.getElementById) {
        preInit(); /* shouldn't be necessary, but IE can sometimes get ahead of itself and trigger fadeInit first */
        galleryImages = new Array;
        var node = gallery.firstChild;
        /* instead of using childNodes (which also gets empty nodes and messes up the script later)
        we do it the old-fashioned way and loop through the first child and its siblings */
        while (node) {
            if (node.nodeType==1) {
                galleryImages.push(node);
            }
            node = node.nextSibling;
        }
        for(i=0;i<galleryImages.length;i++) {
            /* loop through all these child nodes and set up their styles */
            galleryImages[i].style.position='absolute';
            galleryImages[i].style.top=0;
            galleryImages[i].style.zIndex=0;
            /* set their opacity to transparent */
            fader(i,0);
        }
        /* make the list visible again */
        gallery.style.visibility = 'visible';
        /* initialise a few parameters to get the cycle going */
        currentImage=0;
        previousImage=galleryImages.length-1;
        opacity=100;
        fader(currentImage,100);
        /* start the whole crossfade process after a second's pause */
        window.setTimeout("crossfade(100)", 1000);
    }
}
function crossfade(opacity) {
        if (opacity <= 100) {
            /* current image not faded up fully yet...so increase its opacity */
            fader(currentImage,opacity);
            /* fader(previousImage,100-opacity); */
            opacity += 5;
            window.setTimeout("crossfade("+opacity+")", 10);
        } else {
            /* make the previous image - which is now covered by the current one fully - transparent */
            fader(previousImage,0);
            /* current image is now previous image, as we advance in the list of images */
            previousImage=currentImage;
            currentImage+=1;
            if (currentImage>=galleryImages.length) {
                /* start over from first image if we cycled through all images in the list */
                currentImage=0;
            }
            /* make sure the current image is on top of the previous one */
            galleryImages[previousImage].style.zIndex = 0;
            galleryImages[currentImage].style.zIndex = 100;
            /* and start the crossfade after a second's pause */
            opacity=0;
            window.setTimeout("crossfade("+opacity+")", 5000);
        }

}
/* initialise fader by hiding image object first */
addEvent(window,'load',fadeInit)
/* 3rd party helper functions */
/* addEvent handler for IE and other browsers */
function addEvent(elm, evType, fn, useCapture) 
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
} 
//]]>
</script>
<style type="text/css">
#slider {max-height:700px;background: url(&quot;https://lh6.googleusercontent.com/-LLFEz-EyGbk/UyV9SbGPuhI/AAAAAAAAMgY/JNqf8X11dbk/s220/slider-loader.gif&quot;) #2e2e2e no-repeat 50% 50%;}
#gallery {padding:0;position:relative;margin:0 auto;max-width:1920px;}
#gallery li {list-style-type:none;width:100%;display:block;}
.gallery_img img {max-width:100%;}
.gallery_text {width:1000px;margin:0 auto;text-align:center;position:absolute;top:50%;left:50%;margin-left:-500px;margin-top:-110px;}
.gallery_text h2 {padding:0;line-height:70px;font-size:50px;font-weight:inherit;color:#fff;}
.gallery_text p {margin:20px 0;line-height:24px;font-size:20px;color:#ffee66;}
.gallery_text a {background:#77aa00;display:inline-block;padding:20px 70px;font-size:18px;font-weight:700;text-transform:uppercase;color:#fff;text-decoration:none;}
.gallery_text a:hover {background:#fff;color:#000;}
</style>
<div class='clear'/>
<div id='slider'>
<ul id='gallery'>
<li style='position:relative!important;'>
<div class='gallery_img'><img alt='Google' src='https://lh4.googleusercontent.com/-Nh50j1-Bqws/UyV9Pv_wd3I/AAAAAAAAMf8/nsYUnwm35Gs/s1920/slide_1.jpg' title='Google'/></div>
<div class='gallery_text'><h2>Google</h2><p>Google is an American multinational corporation specializing in Internet-related services and products. These include online advertising technologies, search, cloud computing, and software. Most of its profits are derived from AdWords.</p><a href='http://www.google.com'>Open Google</a></div>
</li>
<li>
<div class='gallery_img'><img alt='Bing' src='https://lh4.googleusercontent.com/-eGrPYj9dz1c/UyV9QgDIh5I/AAAAAAAAMgM/mlcDdyufQJs/s1920/slide_2.jpg' title='Bing'/></div>
<div class='gallery_text'><h2>Bing</h2><p>Bing is a search engine that brings together the best of search and people in your social networks to help you spend less time searching and more time doing.</p><a href='http://www.bing.com'>Open Bing</a></div>
</li>
<li>
<div class='gallery_img'><img alt='Yahoo' src='https://lh6.googleusercontent.com/-L_s8vxgupPY/UyV9RKToZeI/AAAAAAAAMgQ/TWs-wy7lbrk/s1920/slide_3.jpg' title='Yahoo'/></div>
<div class='gallery_text'><h2>Yahoo</h2><p>Yahoo! Inc. is an American multinational Internet corporation headquartered in Sunnyvale, California.</p><a href='http://www.yahoo.com'>Open Yahoo</a></div>
</li>
</ul>
</div>
<div class='clear'/>

当您在链接处移动鼠标时,您会看到链接正在显示下一张幻灯片。在这里我也添加代码来查看它。

【问题讨论】:

  • li 滑动时查看您的 html,z-indexopacity 不同步。当前图像的z-index 为100,其opacity 为0,上一个图像的z-index 为0,其opacity = 1
  • @artm 是的,这就是我感到困惑的地方。尝试了很多方法,但无法修复它......

标签: javascript html css slider gallery


【解决方案1】:

http://jsfiddle.net/gwbqvqjs/2/

更改这些行:

galleryImages[previousImage].style.zIndex = 0;
galleryImages[currentImage].style.zIndex = 100;

到这里:

galleryImages[previousImage].style.zIndex = 100;
galleryImages[(currentImage + 1) % galleryImages.length].style.zIndex = 0;

并将其他代码更改为:

opacity=0;
window.setTimeout(function() {
    galleryImages[previousImage].style.zIndex = 0;
    galleryImages[previousImage].style.opacity = 0;
    crossfade(opacity);
}, 5000);

修复它。您的代码比它需要的更复杂,将 currentImage 与 previousImage previousImage=currentImage; 等交换,很难看到发生了什么。尽量简化。

【讨论】:

  • 你抓住了我,解决了我。如果您对这些功能有任何简单的编码,那么我会欢迎...
  • @MuhammadHassan 好的,有时间我会尝试看看,如果我不看,请提醒我。
  • 看看我试过的...这个代码可以接受吗...???演示地址:jsfiddle.net/yyathnom
  • @MuhammadHassan acceptable 确实没有定义,取决于人,但是你有 2 个函数而不是 4-5 个,没有递归调用,所以这是一个改进。你也没有previous=next,所以这也有帮助。不过看起来好多了,做得很好。稍后我会更详细地查看它,看看它是否可以进一步缩短,但看起来要好得多。
  • 谢谢。实际上我讨厌 JQuery,但我知道 JQuery 可以用更少的编码完成所有功能,但我想制作一个没有 JQuery 的全功能滑块。我也不想使用与某些旧浏览器不兼容的您更了解的 CSS3。所以我的下一个目标是在此代码中添加一些导航按钮,以便欢迎您的帮助...
【解决方案2】:

使用这个.. :) &lt;a&gt;position 会改变,但链接会完美跳转。

<a style="position:absolute; margin-left:-50%; top:318px;" href='http://www.google.com'>Open Google</a>
<a style="position:absolute; margin-left:-15%; top:318px;" href='http://www.bing.com'>Open Bing</a>
<a style="position:absolute; margin-left:24%; top:318px;" href='http://www.yahoo.com'>Open Yahoo</a>

祝你好运!

【讨论】:

    猜你喜欢
    • 2014-02-27
    • 2018-06-25
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多