简体   繁体   English

如何使用jQuery淡出主div并淡入相应单击的缩略图中的内容

[英]How to use jQuery to fade out main div and fade in content from corresponding clicked thumbnail

A little new with jQuery...I'm setting up a portfolio page. jQuery的新功能...我正在建立一个投资组合页面。 I have a main div that displays an image and description of a project, then a list of thumbnail texts (brief titles) for all projects. 我有一个主要的div,它显示一个项目的图像和说明,然后显示所有项目的缩略图(简短标题)列表。 This is what I'm trying to do: 这就是我想要做的:

When a specific thumbnail text is clicked, the main div fades out the currently visible project and fades in the project corresponding to the thumbnail. 单击特定的缩略图文本时,主div淡出当前可见的项目,并淡入与缩略图相对应的项目。 What's the best way to use href to load target content into the main display? 使用href将目标内容加载到主显示中的最佳方法是什么?

Can't seem to find a plugin that lets me do this easily. 似乎找不到让我轻松执行此操作的插件。 Been trying to use a bit of javascript from http://perishablepress.com/slide-fade-content . 一直在尝试使用来自http://perishablepress.com/slide-fade-content的JavaScript。

<!--MAIN DISPLAY-->
<div id="main">
<div id="projectA">
<img src="projectA-image.png" />
<div id="projectA">Description of Project A...</div>
</div>

<!--THUMBNAILS-->
<div class="thumb">
<a href="#projectA" class="thumb-text">PROJECT A</a>
</div>

<div class="thumb">
<a href="#projectB" class="thumb-text">PROJECT B</a>
</div>

Here's as far as I've gotten with javascript: 这是我对javascript的了解:

 $('div.thumb-text').click(function () {    
        var href = $(this).attr('href');
        $('div#piece-content').fadeOut("slow", function(){
            $('div#piece-content').load('portfolio.html#' +href);

        });
      });

I see a few issues here. 我在这里看到一些问题。 First, your selector is wrong. 首先,您的选择器是错误的。 div.thumb-text will select all div s with a class of thumb-text . div.thumb-text将选择所有具有thumb-text类的div You have none of those. 你什么都没有。 This should be either div .thumb-text to find all elements within a div with the class of thumb-text or a.thumb-text to get all a tags with the class of thumb-text . 这应该是div .thumb-text找到中的所有元素div与类的thumb-texta.thumb-text让所有a带班的标签thumb-text

2nd, $('div#piece-content').load('portfolio.html#' +href); 第二, $('div#piece-content').load('portfolio.html#' +href); you will end up with two # s in this case since href already has one. 在这种情况下,您将得到两个# ,因为href已经有一个。 And I'm not sure what your intention is here but # is not used to pass things to the backend, you may be intending to use ? 而且我不确定您的意图是什么,但是#不用于将事物传递给后端,您可能打算使用? , not entirely sure what you were going for here. ,不确定要在这里做什么。

And finally, you never fade the element back in. So in your .load you need a callback: 最后,您再也不会褪色该元素。因此,在.load您需要一个回调:

$('div#piece-content').load('portfolio.html#' +href, function(){
   $(this).fadeIn();
});

A few things: you cannot do portfolio.html# (that is a hashtag) but you can do regular jQuery selectors inside the load . 几件事:您不能执行portfolio.html# (这是一个标签),但是可以在load内部执行常规jQuery selectors If you want to get the div with id of photos then use load('portfolio.html #photos') That is not a hash tag, that is a selector, just to make things clear. 如果要获取带有photos iddiv ,请使用load('portfolio.html #photos')这不是一个哈希标签,而是一个选择器,目的只是为了使事情更清楚。

Another thing: a lot of your HTML does not match your jQuery selectors at all. 另一件事:您的许多HTML根本不匹配jQuery选择器。 For example, you reference div#piece-content but I don't see that in your DOM . 例如,您引用了div#piece-content但我在您的DOM没有看到它。 Same with div.thumb-text , it looks like you want $('div.thumb').children('a.thumb-text') instead. div.thumb-text相同,看起来您想要$('div.thumb').children('a.thumb-text')代替。

Thirdly: assuming div.piece-content is the main div you were talking about, simply calling a fadeOut from what I understand, will not work. 第三:假设div.piece-content是您正在谈论的主要div,仅根据我的理解调用一个fadeOut将不起作用。 It will simply set its opacity to 0 and then it will load content into it. 它将简单地将其不透明度设置为0,然后将内容加载到其中。 You should do your fadeOut() , then load the content then call fadeIn() 您应该先执行fadeOut() ,然后加载内容, 然后调用fadeIn()

$('div.thumb-text').click(function() {    
    var href = $(this).attr('href');
        $('div#piece-content').fadeOut("slow", function(){
            $(this).load('portfolio.html #' +href', {}, function() {
                $(this).fadeIn();
            });
    })
});

Actually, I would definitely dig deeper in Google for existing plugins, especially if you are a beginner with jQuery. 实际上,我肯定会更深入地研究Google现有插件,特别是如果您是jQuery的初学者。

Maybe the Nivo Slider could be something? 也许Nivo Slider可能是什么?

Thanks all, here's what I ended up with in javascript: 谢谢大家,这是我在javascript中最终得到的结果:

var activeDiv=$('#project-A'); //set activeDiv to the first piece you want to show 

              $('a.thumb-text').click(function () {     

                var href = $(this).attr('href'); //grabs the href from <a class="thumb-text">


                    activeDiv.fadeOut("fast", function() { //fades out the currently showing piece
                        $(href).fadeIn("slow");
                        activeDiv = $(href); //set the active piece to what was clicked on

                    });

              });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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