在圖像上淡入文本 (Fade in text over an image)


問題描述

在圖像上淡入文本 (Fade in text over an image)

Two things don't seem to work with this code.

  1. I want to be able to hover the larger DIV in order to bring the smaller one into view.

As it stands, it only works if you hover over the smaller DIV. 2. The smaller DIV doesn't disappear when I stop hovering.

<div class="one">
    <div class="two"></div>
</div>

<div class="one">
    <div class="two"></div>
</div>

.one {
    position: relative;
    width: 100px;
    height: 100px;
    margin: 10px;
    background-color: #CCC;
}
.two {
    position: relative;
    top: 20px;
    left: 20px;
    height: 40px;
    width: 40px;
    background: #333;
}

/* Fade-in text over images */
$(function(){
    $(".two").css("opacity",0).fadeTo(0, 0);
    $(".two").mouseover(function () {
        $(this).fadeTo(200, 1);
    });
    $("two").mouseout(function () {
        $(this).fadeTo(200, 0);
    });
});
----- ## 參考解法 #### 方法 1:

You should target the larger div, .one, and then change the smaller div, .two within the context of this whenever hovering .one. When fading from visible to invisble you can most of the time use fadeIn/Out, and just set the element to display:none in the CSS.

$(function(){
    $('.one').on({
        mouseenter: function() {
            $(".two", this).fadeIn(200);
        },
        mouseleave: function() {
            $(".two", this).fadeOut(200);        
        }
    });
});​

FIDDLE

方法 2:

jsFiddle demo

$(".one").on('mouseenter mouseleave',function ( e ) {
      var fade = e.type=='mouseenter'?
      $('.two', this).stop().fadeTo(200, 1):
      $('.two', this).stop().fadeTo(200, 0);
});

方法 3:

That's because you are selecting smaller div tags.  

$(function() {
    $(".two").hide();
    $(".one").hover(function() {
        $('.two', this).fadeIn(200);
    }, function() {
        $('.two', this).fadeOut(200);
    });
});

Fiddle

  • hide()
  • hover()
  • fadeIn()
  • fadeOut()

(by AndyadeneoRoko C. BuljanRam)

參考文件

  1. Fade in text over an image (CC BY-SA 3.0/4.0)

#jquery #css






相關問題

讓 jQuery 與 Netscape 7 和 8 一起工作 (Getting jQuery to work with Netscape 7 and 8)

使用 Jquery 的 mvc3 搜索結果 (mvc3 search results with Jquery)

從嵌套的 jquery 函數返回一個值 (Return a value from nested jquery function)

Mencocokkan lebar divisi dengan jquery (Matching division widths with jquery)

無法在 jQuery AJAX 中多次生成點擊事件 (unable to generate click event more than once in jQuery AJAX)

使用雙引號格式並用逗號分隔元素數組 (Implode an element array with double quote format and separated by comma)

選擇不更新 (Select doesn't update)

chrome中帶有省略號的多行文本問題 (issue with multiline text with ellipsis in chrome)

AJAX/PHP/JS - 無法將頁面內容加載到容器中 (AJAX/PHP/JS - Cannot load page content into container)

使用 jQuery 將文本插入 textarea (Insert text into textarea with jQuery)

滾動到頁面底部,僅當用戶在 DOM 操作之前已經位於底部時 (Scroll to bottom of page, only if the user already was at the bottom before DOM manipulation)

如何設置單選按鈕的樣式,使其看起來像普通的可點擊按鈕? (How do I style a radio button to look like a normal clickable button?)







留言討論