官术网_书友最值得收藏!

Time for action – making it fancy

Perform the following steps to add some fancy new features to the FAQ page:

  1. Let's start with a little CSS code to change the cursor to a pointer and add a little hover effect to our questions to make it obvious to site visitors that the questions are clickable. Open up the styles.css file that's inside the styles folder and add the following bit of CSS code:
    .jsOn dt {
      cursor: pointer;
    }
    
    .jsOn dt:hover {
      color: #ac92ec;
    }

    We're only applying these styles for those site visitors that have JavaScript enabled. These styles definitely help to communicate to the site visitor that the questions are clickable. You might also choose to change something other than the font color for the hover effect. Feel free to style your FAQ list however you'd like. Have a look at the following screenshot:

  2. Now that we've made it clear that our <dt> elements can be interacted with, let's take a look at how to show the answers in a nicer way. When we click on a question to see the answer, the change isn't communicated to the site visitor very well; the jump in the page is a little disconcerting and it takes a moment to realize what just happened. It would be nicer and easier to understand if the questions were to slide into view. The site visitor could literally see the question appearing and would understand immediately what change just happened on the screen.

    jQuery makes that easy for us. We just have to replace our call to the toggle method with a call to the slideToggle method:

    $('dt').on('click', function(){
      $(this).next().slideToggle();
    });

    Now if you view the page in your browser, you can see that the questions slide smoothly in and out of view when the question is clicked. It's easy to understand what's happening when the page changes, and the animation is a nice touch.

  3. Now, there's just one little detail we've still got to take care of. Depending on how you've styled your FAQ list, you might see a little jump in the answer at the end of the animation. This is caused by some extra margins around the <p> tags inside the <dd> element. They don't normally cause any issues in HTML, and browsers can figure how to display them correctly. However, when we start working with animation, sometimes this becomes a problem. It's easy to fix. Just remove the top margin from the <p> tags inside the FAQ list as follows:
    .content dd p {
      margin-top: 0;
    }

    If you refresh the page in the browser, you'll see that the little jump is now gone and our animation smoothly shows and hides the answers to our questions.

What just happened?

We replaced our toggle method with the slideToggle method to animate the showing and hiding of the answers. This makes it easier for the site visitor to understand the change that's taking place on the page. We also added some CSS to make the questions appear to be clickable to communicate the abilities of our page to our site visitors.

主站蜘蛛池模板: 梨树县| 平果县| 黄大仙区| 定安县| 晋城| 山东| 石渠县| 萨迦县| 日土县| 广平县| 威远县| 闸北区| 清原| 罗田县| 游戏| 尉氏县| 梁河县| 万荣县| 陵川县| 永登县| 富顺县| 申扎县| 甘谷县| 加查县| 尚志市| 上虞市| 安阳县| 商河县| 化德县| 松原市| 资中县| 石家庄市| 长春市| 乌拉特中旗| 安顺市| 罗山县| 巢湖市| 南汇区| 永德县| 新野县| 乌拉特后旗|