我有一个左菜单,它是一个ul,我希望它在屏幕上是静态的。滚动时,应始终显示完整菜单。有什么办法可以解决这个问题。
我试图设置'position:fixed; top:0,left:0',但它会将ul旁边的div移到ul的顶部。
<div>
<!-- Sidebar -->
<ul class='anvbar-nav'>
<!-- ...some other stuff-->
</ul>
<!-- Content Wrapper -->
<div class='content'>
<!-- ...some other stuff-->
</div>
我想将ul固定在左上角。但是,当我在ul上添加“ position:fixed”时,带有内容的div会在ul的顶部移动。我该如何解决此问题。我想让div内容占据屏幕上剩余的剩余空间,而不是放在左菜单(ul)顶部
答案 0 :(得分:1)
您也可以通过https://www.w3schools.com/howto/howto_css_fixed_sidebar.asp来登录w3schools
左侧全高
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: "Lato", sans-serif;
}
.sidenav {
height: 100%;
width: 160px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 20px;
}
.sidenav a {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
}
.sidenav a:hover {
color: #f1f1f1;
}
.main {
margin-left: 160px; /* Same as the width of the sidenav */
font-size: 28px; /* Increased text to enable scrolling */
padding: 0px 10px;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
</style>
</head>
<body>
<div class="sidenav">
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#clients">Clients</a>
<a href="#contact">Contact</a>
</div>
<div class="main">
<h2>Sidebar</h2>
<p>This sidebar is of full height (100%) and always shown.</p>
<p>Scroll down the page to see the result.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>
</body>
</html>
左侧导航栏具有自动高度
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: "Lato", sans-serif;
}
.sidenav {
width: 130px;
position: fixed;
z-index: 1;
top: 20px;
left: 10px;
background: #eee;
overflow-x: hidden;
padding: 8px 0;
}
.sidenav a {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 25px;
color: #2196F3;
display: block;
}
.sidenav a:hover {
color: #064579;
}
.main {
margin-left: 140px; /* Same width as the sidebar + left position in px */
font-size: 28px; /* Increased text to enable scrolling */
padding: 0px 10px;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
</style>
</head>
<body>
<div class="sidenav">
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#clients">Clients</a>
<a href="#contact">Contact</a>
</div>
<div class="main">
<h2>Auto Sidebar</h2>
<p>This sidebar is as tall as its content (the links), and is always shown.</p>
<p>Scroll down the page to see the result.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
<p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>
</body>
</html>
答案 1 :(得分:0)
我认为这会对您有所帮助
<?php
/*
Schema of table used to test below code
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| title | varchar(128) | YES | | NULL | |
| link | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| jobdate | datetime | YES | | NULL | |
| city | varchar(32) | YES | | NULL | |
| state | varchar(32) | YES | | NULL | |
| country | varchar(32) | YES | | NULL | |
| company | varchar(64) | YES | | NULL | |
| requirements | text | YES | | NULL | |
| category | varchar(128) | YES | | NULL | |
| experience | text | YES | | NULL | |
| salary | text | YES | | NULL | |
| jobid | int(11) unsigned | NO | | NULL | |
+--------------+------------------+------+-----+---------+-------+
*/
/* utility to get item from XPath query relative to $obj */
function nodevalue( $xp, $obj, $item ){
return $xp->query($item,$obj)->item(0)->textContent ?: false;
}
$errors=array();
$affectedRow = 0;
$url='https://ngcareers.com/xmlfeed.xml';
/* create DOMDodument - use our own error handling */
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->load( $url );
libxml_clear_errors();
/* create the XPath object for queryng the DOM */
$xp=new DOMXPath( $dom );
/* Get a list of all jobs */
$col=$xp->query( '//jobs/job' );
if( $col->length > 0 ){
/* create the basic SQL insert statement for use in a `prepared statement` */
$sql='insert into `requirement`
( `title`, `link`, `description`, `jobdate`, `city`, `state`, `country`, `company`, `requirements`, `category`, `experience`, `salary`, `jobid` )
values
( ?,?,?,?,?,?,?,?,?,?,?,?,? )';
/* create the prepared statement */
$stmt=$con->prepare( $sql );
if( $stmt ){
/* Bind the placeholders to variables */
$stmt->bind_param('ssssssssssssi',
$title,
$link,
$description,
$date,
$city,
$state,
$country,
$company,
$requirements,
$category,
$experience,
$salary,
$id
);
/* Iterate through all the jobs */
foreach( $col as $index => $job ){
/* assign as variables each item from the job */
$title=nodevalue( $xp, $job, 'title' );
$link=nodevalue( $xp, $job, 'url' );
$description=htmlentities( nodevalue( $xp, $job, 'description' ) );
$date=date( 'Y-m-d H:i:s', strtotime( nodevalue( $xp, $job, 'date' ) ) );
$city=nodevalue( $xp, $job, 'city' );
$state=nodevalue( $xp, $job, 'state' );
$country=nodevalue( $xp, $job, 'country' );
$company=nodevalue( $xp, $job, 'company' );
$requirements=htmlentities( nodevalue( $xp, $job, 'requirements' ) );
$category=nodevalue( $xp, $job, 'category' );
$experience=nodevalue( $xp, $job, 'experience' );
$salary=nodevalue( $xp, $job, 'salary' );
$id=nodevalue( $xp, $job, 'id' );
/* execute the INSERT statement */
$status=$stmt->execute();
if( $status )$affectedRow++;
else $errors[]=array(
'error'=>$stmt->error,
'code'=>$stmt->errno,
'index'=>$index,
'title'=>$title,
'jobid'=>$id
);
}
echo $affectedRow==$col->length ? sprintf('All %d records inserted', $col->length ) : sprintf( 'inserted %d of %d rows', $affectedRow, $col->length );
if( !empty( $errors ) ){
printf('<pre><h2>Errors</h2>%s</pre>',print_r( $errors, true ) );
}
} else {
echo "Failed to prepare SQL";
}
}
?>
body {
font-family: "Lato", sans-serif;
}
.sidenav {
height: 100%;
width: 160px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
padding-top: 20px;
}
.sidenav a {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
}
.sidenav a:hover {
color: #f1f1f1;
}
.main {
margin-left: 160px; /* Same as the width of the sidenav */
font-size: 28px; /* Increased text to enable scrolling */
padding: 0px 10px;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
答案 2 :(得分:0)
指定左侧导航宽度,并为内容div赋予与margin-left相同的值。 查看代码段。
[jira.customfield_NNNNN]
name = "My Sprint Status"
:root {--leftnavwidth :250px;}
ul{
margin:0;
padding:0;
list-style:none;
}
li{
padding:10px 20px;
}
.anvbar-nav{
position:fixed;
left:0;
top:0;
width:var(--leftnavwidth);
background:#564897;
color:#fff;
height:100%;
}
.content{
margin-left:var(--leftnavwidth);
}
答案 3 :(得分:0)
margin:auto;
display:inline-block;
答案 4 :(得分:-1)
您可以将包装器添加到元素,使单个项目可滚动。如果使用flexbox,则无论菜单宽度如何,都可以使用它!
public class DashBoardBannerAdapter extends FragmentStatePagerAdapter {
public static final String TAG = "DashBoardBannerAdapter";
public List<DashBoardResponse.Banners> bannersList;
public List<Fragment_Banner> mFragments = new ArrayList<>();
public DashBoardBannerAdapter(FragmentManager fm) {
super(fm);
}
public void addItems(List<DashBoardResponse.Banners> bannersList) {
this.bannersList=bannersList;
Log.d(TAG, "addItems: fragment size before"+mFragments.size());
mFragments.clear();
Log.d(TAG, "addItems: fragment size after"+mFragments.size());
for (int i = 0; i < bannersList.size(); i++)
{
Log.d(TAG, "addItems: "+i);
Bundle bundle = new Bundle();
bundle.putString("imageurl", bannersList.get(i).getImage());
Fragment_Banner fragment_promotion=new Fragment_Banner();
fragment_promotion.setArguments(bundle);
mFragments.add(fragment_promotion);
}
notifyDataSetChanged();
}
@Override
public Fragment getItem(int position)
{
return mFragments.get(position);
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
Object fragment = super.instantiateItem(container, position);
mFragments.set(position, (Fragment_Banner) fragment);
return fragment;
}
@Override
public int getCount() {
return bannersList.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
// TODO Auto-generated method stub
super.destroyItem(container, position, object);
}
}
//Fragment
public class Fragment_Banner extends Fragment
{
public static final String TAG = "Fragment_Banner";
View view;
ImageView ivOnboarderImage;
String imageurl;
public static Fragment_Banner newInstance() {
Bundle args = new Bundle();
Fragment_Banner fragment = new Fragment_Banner();
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
view = inflater.inflate(R.layout.fragment_banner, container, false);
imageurl = getArguments().getString("imageurl");
ivOnboarderImage =view.findViewById(R.id.iv_image);
ivOnboarderImage.setImageDrawable(null);
ivOnboarderImage.setVisibility(View.VISIBLE);
if (imageurl != null) {
Picasso.Builder builder = new
Picasso.Builder(getActivity().getApplicationContext());
builder.listener(new Picasso.Listener() {
@Override
public void onImageLoadFailed(Picasso picasso, Uri uri,
Exception exception) {
}
});
Picasso pic = builder.build();
pic.load(imageurl)
.fit()
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.placeholder(R.drawable.ic_launch)
.error(R.drawable.ic_email)
.into(ivOnboarderImage);
}
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}}
body {
display: flex;
}
.navbar-wrapper {
height: 100vh;
flex: 0;
background-color: lightblue;
padding: 12px;
}
.wrapper {
height: 100vh;
margin: 0;
flex: 1;
background-color: #eee;
padding: 12px;
}
.wrapper > .content {
overflow: scroll;
}