2016-12-13 30 views
-3

メニューボタンを押したときにヘッダー段落が一定の幅(たとえば1024ピクセル幅)で消えてしまいたいです。 Jqueryコードを追加すると、ブラウザはJqueryコードにコメントすると存在しないエラー(ReferenceError:openSideは定義されていません)を返します。 jQueryの徹底的に書き込まれている場合、私は今はありませんが、これまでのところ、私はこの問題を通じてjavascriptでJqueryコードが機能しない

HTML行くことができない。

<div id="header"> 

     <header id="title"> 
      <h1 style="font-size: 70px; font-weight: 600">The Nest</h1> 
      <p style="font-size: 40px">The hostel where your journey should start</p> 

      <button type="button" class="btn btn-lg" id="menu-btn" onclick="openSide()"> 
      <span class="glyphicon glyphicon-list"></span> 
      </button> 

     </header> 

    </div> 

    <div id="sidebar"> 
     <a href="javascript:void(0)" onclick="closeSide()">&times;</a> 
     <a href="#">Accomodations</a> 
     <a href="#">Services</a> 
     <a href="#">Merchandising</a> 
     <a href="#">About us</a> 
    </div> 

Javascriptを:あなたの助け

ため

function openSide() { 
document.getElementById("sidebar").style.width = "350px"; 
//document.getElementById("header").style.backgroundPosition = "-250px"; 
document.getElementById("title").style.marginRight = "350px"; 
//document.getElementById("header").style.background = "linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5))"; 
} 

function closeSide() { 
document.getElementById("sidebar").style.width = "0"; 
//document.getElementById("header").style.backgroundPosition = "0px"; 
document.getElementById("title").style.marginRight = "0px"; 
} 

var main = function() { 

if (max-width = 1024px) { 
$("#menu-btn").click(function() { 
    $("#title p").hide(); 
}); 
} 

} 

$(document).ready(main); 

THXを

+0

なぜインラインイベントハンドラとjqueryイベントハンドラを最初に混ぜているのですか? – charlietfl

+0

構文エラー: 'if(max-width = 1024px)'。たぶん 'if(max-width == '1024px')'であるべきです。 –

+0

私はサイドナビバーを表示して消滅させるためにJavascriptを使い始めましたが、Jqueryが追加されてもJavascriptコードはそれ以上動作しません – Dema

答えて

0

等価演算子の代わりに代入演算子を使用しています。あなたのコードは次のようになります。

$(document).ready(() => { 
 
    // Your condition here 
 
    if (true) { 
 
    $('#menu-btn').click(function() { 
 
     $('#title').hide(); 
 
    }); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="menu-btn">Hide content</button> 
 
<div id="title">foo</div>

ところで、あなたが投稿コードで、変数max-widthが有効な変数名ではなく、また、あなたがそれを定義されていません。

+1

' max-width'は変数ではありません。それは有効な変数名ではないので、変数にすることはできません。 – Xufox

+0

ThxMatìas、私は "max-width"が変数でなければならないことを理解しました(そうでなければ意味をなさない)。なぜあなたはjavascriptコードがうまくいかないと思いますか? – Dema

+0

@Dema変数名に '-'をつけることはできません。 'max_width'または' maxWidth'を使います。 – Barmar

関連する問題