2017-08-20 3 views
0

私がしようとしているのは、divという名前のイベントが呼び出されたときにトップまたはトップのコンテンツが特定のサイズに隠れて見えます。私はそれをする方法を考えることができません。jQueryまたはjavascriptを使用してdivの高さを上から下げる

次のコードは、高さを下から上げ下げしますが、上から下げたいものです。

$(function() { 
 

 
    $("button:first").on("click", function() { 
 
    $("#target").css("height", "+=50px"); 
 
    }); 
 

 
    $("button:last").on("click", function() { 
 
    $("#target").css("height", "-=50px"); 
 
    }); 
 
});
#target { 
 
    background: purple; 
 
    color: white; 
 
    height: 300px; 
 
    width: 200px; 
 
} 
 

 
div { 
 
    float: left; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div> 
 
    <button>PLUS</button> 
 
    <button>MINUS</button> 
 
</div> 
 
<div id="target"> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
 
    dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 
 
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure 
 
    dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> 
 

 
</div>

+0

は、コードが動作していないということですか? ?ユーザーがボタンをクリックしたときに機能していますか? –

+0

それは働いていますが、底からの高さを減らしたり追加したりしますが、これを上から減らしたいのは – Harvey

+0

jsbinでコードをアップロードできますか?あなたのコンテンツから切り離した場合、 –

答えて

0

あなたは、要素の現在の高さを取得し、それにどんな計算を行うし、新しいCSS値を設定する必要があります。

P.S. これはブラウザがコンテンツをレンダリングする方法であるため、常に下から追加したり減らしたりします。左上から右下へ。したがって、高さを追加または減算すると、下から表示されます。

これは動作するはずです。

$("button:first").on("click", function() { 
    var targ = $("#target"), 
      targHt = targ.height(), 
      newTargHt = targHt + 50; 

    targ.height(newTargHt); 
}); 

$("button:last").on("click", function() { 
    var targ = $("#target"), 
      targHt = targ.height(), 
      newTargHt = targHt - 50; 

    targ.height(newTargHt); 
}); 
+0

あなたのコードは私のコードが何をするかを...私はそのブラウザを知っているコンテンツをレンダリングします。左上から右下に..しかし、私は誰かがその周りに道を持っていることを願っています: – Harvey

+0

JSから制御できるものではない私の理解を形成してください。 C++のような言語が必要です。 –

+0

あなたの時間に感謝します...あなたは部分的に正しい...いくつかのCSSはトリックをしました – Harvey

0

ちょっとこのビンの表情を持っている:https://jsbin.com/wiqapadeza/edit?html,js,output 私はあなたの問題を解決するためのマージンプロパティを使用しています

$(function() { 
var top=0; 
$("button:first").on("click", function() { 
    top=top+50; 
    $("#target").css("height", "+=50px"); 
    $("p").css("margin-top", top); 
    }); 
$("button:last").on("click", function() { 
    top=top-50; 
    $("#target").css("height", "-=50px"); 
    $("p").css("margin-top", top); 
}); 
}); 

私はそれがあなたの役に立てば幸い:

+0

これはあなたを助けましたか? –

+0

@harveyそれは働いた –

+0

そこに私がマイナス – Harvey

関連する問題