सीएसएस: x- अक्ष पर निश्चित स्थिति लेकिन y नहीं?


104

क्या एक्स-अक्ष पर केवल एक स्थिति को ठीक करने का एक तरीका है? इसलिए जब कोई उपयोगकर्ता स्क्रॉल करता है, तो div टैग उसके साथ स्क्रॉल करेगा, लेकिन साइड साइड नहीं?


3
क्या यह ठीक है, अगर मैंने इस समस्या का एक जेएस समाधान दिया है?
स्टारएक्स

1
ऐसा करने के लिए आपको जावास्क्रिप्ट की आवश्यकता है।
लॉस्टलिन

@ स्टार्क्स - यह न केवल ठीक है, यह आवश्यक है :-)
पीट विल्सन

मैंने हाल ही में एक ऐसे ही सवाल का जवाब दिया है जो आपके लिए देख रहे हैं की तर्ज पर अधिक हो सकता है: stackoverflow.com/questions/8673560/…
Wex

मुझे इसका बहुत पुराना सवाल पता है, लेकिन क्या आपको अभी भी इस सवाल से कोई समस्या है?
स्टारएक्स

जवाबों:


58

स्क्रिप्ट का उपयोग करके इसकी एक सरल तकनीक भी। आप यहां डेमो देख सकते हैं भी हैं।

JQuery

$(window).scroll(function(){
    $('#header').css({
        'left': $(this).scrollLeft() + 15 
         //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to remain at 15px left
    });
});

सीएसएस

#header {
    top: 15px;
    left: 15px;
    position: absolute;
}

क्रेडिट अपडेट करें : @ पियरेडेलेसिन

जैसा कि टिप्पणी की गई है, स्क्रिप्ट को स्क्रिप्ट में पुन: व्यवस्थित किए बिना सीएसएस में परिवर्तन का समर्थन करने के लिए। आप निम्नलिखित का उपयोग कर सकते हैं।

var leftOffset = parseInt($("#header").css('left')); //Grab the left position left first
$(window).scroll(function(){
    $('#header').css({
        'left': $(this).scrollLeft() + leftOffset //Use it later
    });
});

डेमो :)


@PeteWilson, ठीक है इस समाधान की जाँच करें
Starx

2
आप leftफ़ंक्शन की शुरुआत में मान प्राप्त करने के बारे में क्या सोचते हैं जैसे init_left = $('#header').position()['left']या कुछ इसी तरह?
पियरे डे LESPINAY

@PierredeLESPINAY, हां, बहुत अच्छी बात है। यह परिवर्तनों को समर्थन देने के लिए पर्याप्त लचीला बना देगा।
स्टारएक्स

2
मैं इस समाधान से प्यार करता हूं, और बस इसे खुद इस्तेमाल किया। एक समस्या मुझे इसके साथ है हालांकि यह है कि जब आप चिकनी स्क्रॉलिंग करते हैं तो जलपान एक प्रकार का होता है (जैसे: स्क्रॉलिंग को खींचकर)। ऐसा लगता है कि जावास्क्रिप्ट ताज़ा सीएसएस ताज़ा की तुलना में धीमी है। इसके लिए कोई उपाय?
jsarma

17

यह एक पुराना धागा है लेकिन CSS3 का एक समाधान है।

position: sticky;

इस ब्लॉग पोस्ट पर एक नज़र है।

प्रदर्शन:

सीएसएस स्थिति चिपचिपा एनीमेशन

और यह प्रलेखन।


1
इसके लिए एक समाधान खोजने में बिजी उम्र, आपका जवाब आदर्श रूप से मेरी जरूरतों को पूरा करता है।
क्रिस्टोफर शॉ

6
यह ऊर्ध्वाधर सामान के लिए अच्छा है, लेकिन क्षैतिज स्क्रॉलिंग के लिए काम नहीं करता है।
कलुब

10
इस सवाल का जवाब बिल्कुल नहीं है।
Alohci

12

यदि आपका ब्लॉक मूल रूप से स्थिर है, तो आप इसे आज़माना चाहते हैं

$(window).on('scroll', function () {

  var $w = $(window);
  $('.position-fixed-x').css('left', $w.scrollLeft());
  $('.position-fixed-y').css('top', $w.scrollTop());

});
.container {
  width: 1000px;
}

.position-fixed-x {
  position: relative; 
}

.position-fixed-y {
  position: relative;
}

.blue-box {
  background:blue;
  width: 50px;
  height: 50px;
}

.red-box {
  background: red;
  width: 50px;
  height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

<div class="position-fixed-y red-box">
  
</div>

The pattern of base pairs in the DNA double helix encodes the instructions for building the proteins necessary to construct an entire organism. DNA, or deoxyribonucleic acid, is found within most cells of an organism, and most organisms have their own unique DNA code. One exception to this is cloned organisms, which have the same exact DNA code as their parents do.

<div class="position-fixed-x blue-box">
  
</div>

DNA strands are composed of millions of sub-units, called nucleotides. Each nucleotide contains a 5-carbon sugar, a phosphate group and a nitrogen base. There are four different variations of the nitrogen base group, responsible for all of the variation between two different DNA strands. The four different variations are called adenine, guanine, cytosine and thymine, but they are typically abbreviated and only referred to by their first letter. The sequence of these different nitrogen bases makes up the code of the DNA.

The DNA strand splits in two, and forms two different DNA strands during cell replication. However, sometimes this process is not perfect, and mistakes occur. These mistakes may change the way an organism is constructed or functions. When this happens, it is called a mutation. These mutations can be helpful or harmful, and they are usually passed on to the organism’s offspring.
  
 The traits of a living thing depend on the complex mixture of interacting components inside it. Proteins do much of the chemical work inside cells, so they largely determine what those traits are. But those proteins owe their existence to the DNA (deoxyribonucleic acid), so that is where we must look for the answer.
The easiest way to understand how DNA is organized is to start with its basic building blocks. DNA consists of four different sugars that interact with each other in specific ways. These four sugars are called nucleotide bases and have the names adenine (A), thymine (T), cytosine (C) and guanine (G). Think of these four bases as letters in an alphabet, the alphabet of life!
If we hook up these nucleotides into a sequence--for example, GATCATCCG--we now have a little piece of DNA, or a very short word. A much longer piece of DNA can therefore be the equivalent of different words connected to make a sentence, or gene, that describes how to build a protein. And a still longer piece of DNA could contain information about when that protein should be made. All the DNA in a cell gives us enough words and sentences to serve as a master description or blueprint for a human (or an animal, a plant, or a microorganism).
Of course, the details are a little more complicated than that! In practice, active stretches of DNA must be copied as a similar message molecule called RNA. The words in the RNA then need to be "read" to produce the proteins, which are themselves stretches of words made up of a different alphabet, the amino acid alphabet. Nobel laureates Linus Pauling, who discerned the structure of proteins, and James Watson and Francis Crick, who later deciphered the helical structure of DNA, helped us to understand this "Central Dogma" of heredity--that the DNA code turns into an RNA message that has the ability to organize 20 amino acids into a complex protein: DNA -> RNA -> Protein.
To understand how this all comes together, consider the trait for blue eyes. DNA for a blue-eyes gene is copied as a blue-eyes RNA message. That message is then translated into the blue protein pigments found in the cells of the eye. For every trait we have--eye color, skin color and so on--there is a gene or group of genes that controls the trait by producing first the message and then the protein. Sperm cells and eggs cells are specialized to carry DNA in such a way that, at fertilization, a new individual with traits from both its mother and father is created.
</div>


8

नहीं, यह शुद्ध CSS के साथ संभव नहीं है। इस तरह की स्थिति व्यूपोर्ट में तत्व को ठीक करती है। मेरा सुझाव है कि व्यूपोर्ट के दोनों किनारों (यानी ऊपर, नीचे, बाएं या दाएं) में से एक को तत्व को ठीक करना चाहिए ताकि यह अन्य सामग्री के साथ हस्तक्षेप न करे।


7

अब जब मोबाइल इंटरनेट बाजार से 70% से अधिक है, तो आप ऐसा करने के लिए कुछ स्मार्ट और उत्तरदायी बना सकते हैं।

आप इसे केवल सीएसएस के साथ बहुत आसान बना सकते हैं, एक अतिप्रवाह-एक्स का उपयोग करें: एक कंटेनर के लिए स्क्रॉल करें और एक अतिप्रवाह-वाई: दूसरे कंटेनर में स्क्रॉल करें। आप कंटेनर तत्वों को चौड़ाई के साथ आसानी से रख सकते हैं: 100vw और ऊँचाई: 100vh।

इसका परीक्षण करने के लिए उदाहरण पर मध्य क्लिक करें। मोबाइल पर सबसे अच्छा काम करता है क्योंकि आप स्क्रॉल बार नहीं देखते हैं।

body{max-width:100%}
*{box-sizing:border-box;}
.container{background:#ddd;overflow-y:scroll;width:500px;max-height:100vh;}
.header{background: pink;}
.body{background: teal;padding:20px;min-width: 100%;overflow:scroll;overflow-y:hidden;min-height:300px;}
.body >div{min-width:800px;}
<body>
  <div class="container">
    <div class="header">
       Button 1 > Button 2 > Button 3
    </div>
    <div class="body">
      <div>
Lorem ipsum dolor sit amet, consectetur adipiscing 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
<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing 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
<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing 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<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing 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<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing 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<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing 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<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing 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<br><br>
Lorem ipsum dolor sit amet, consectetur adipiscing 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
     </div>
    </div>
  </div>
</body>


मैं नीचे स्क्रॉलबार को केवल पैरों पर स्क्रॉल करने के लिए दिखाई नहीं दे रहा ...
फंकी

मोबाइल ब्राउज़र पर यह दिखाई नहीं देगा। Chrome पर आप इसे इस CSS से छिपा सकते हैं .yourclass::-webkit-scrollbar-track, .yourclass::-webkit-scrollbar-thumb {display:none;}। फ़ायरफ़ॉक्स पर आप इसे overflow:hiddenकंटेनर के बाहर रख सकते हैं । लेकिन इससे पहले कि एक और सवाल का जवाब दिया गया है, मुझे यकीन है।
EPurpl3

2

स्टारएक्स का समाधान मेरे लिए बेहद मददगार था। लेकिन मुझे कुछ समस्याएं हुईं जब मैंने इसके साथ एक ऊर्ध्वाधर स्क्रॉलिंग साइडबार को लागू करने की कोशिश की। यहाँ मेरा प्रारंभिक कोड था, जो स्टारएक्स ने लिखा था:

function fix_vertical_scroll(id) {
    $(window).scroll(function(){
        $(id).css({
            'top': $(this).scrollTop() //Use it later
        });
    });
}

यह Starx के समाधान से थोड़ा अलग है, क्योंकि मुझे लगता है कि उसका कोड एक मेनू को लंबवत के बजाय क्षैतिज रूप से तैरने की अनुमति देने के लिए डिज़ाइन किया गया है। लेकिन यह सिर्फ एक तरफ है। उपरोक्त कोड के साथ मुझे जो समस्या थी वह यह है कि बहुत सारे ब्राउज़रों में, या कंप्यूटर के संसाधन भार के आधार पर, मेनू चालन चॉपी होगी, जबकि प्रारंभिक सीएसएस समाधान अच्छा और चिकना था। मैं इसे सीएसएस को लागू करने की तुलना में जावास्क्रिप्ट घटनाओं को कम करने में ब्राउज़रों के लिए धीमी गति से करता हूं।

इस चंचलता समस्या के लिए मेरा वैकल्पिक समाधान फ्रेम को पूर्ण के बजाय तय किया गया है, फिर स्टार्क्स की विधि का उपयोग करके क्षैतिज आंदोलनों को रद्द करें।

function float_horizontal_scroll(id) {
    jQuery(window).scroll(function(){
        jQuery(id).css({
            'left': 0 - jQuery(this).scrollLeft()
        });
    });
}

#leftframe {
 position:fixed;
 width: 200;
}   

आप कह सकते हैं कि मैं जो सब कर रहा हूं वह क्षैतिज स्क्रॉलिंग तड़का के लिए ऊर्ध्वाधर स्क्रॉलिंग चॉपीनेस का व्यापार कर रहा है। लेकिन बात यह है कि, 99% स्क्रॉलिंग ऊर्ध्वाधर है, और जब क्षैतिज स्क्रॉलिंग की तुलना में यह तड़का हुआ होता है तो यह बहुत अधिक कष्टप्रद होता है।

यहाँ इस विषय पर मेरी संबंधित पोस्ट है, अगर मैंने पहले से ही सबके धैर्य को समाप्त नहीं किया है: एक दिशा में एक मेनू फिक्सिंग jquery


2

मैं अपने हेडर / नेविगेशन बार को केंद्रित रखने और आकार में बदलाव के लिए उत्तरदायी रखने के लिए एक अच्छा तरीका ढूंढ रहा था।

//CSS
.nav-container {
  height: 60px;  /*The static height*/
  width: 100%;  /*Makes it responsive to resizing the browser*/
  position: fixed;  /*So that it will always be in the center*/
}

//jQuery
$(window).scroll(() => {
  if ($(document).scrollTop() < 60) {
    $('.nav-container').css('top', $(document).scrollTop() * -1)
  }
})

जैसे-जैसे हम स्क्रॉल करते हैं, बार स्क्रीन से ऊपर की ओर बढ़ता है। यदि आप बाएं / दाएं स्क्रॉल करते हैं तो यह स्थिर रहेगा।


धन्यवाद भाई। यह काम किया। इतना सरल हालांकि !! जीनियस यू
राम

0

मुझे लगता है कि यह धागा शक्तिशाली पुराना है, लेकिन इसने मुझे अपनी परियोजना के लिए एक समाधान के साथ आने में मदद की।

मेरे मामले में मेरे पास एक हेडर था जिसे मैं कभी भी 1000px से कम चौड़ा नहीं करना चाहता था, हेडर हमेशा शीर्ष पर होता है, ऐसी सामग्री के साथ जो असीमित अधिकार तक जा सकती है।

header{position:fixed; min-width:1024px;}
<header data-min-width="1024"></header>

    $(window).on('scroll resize', function () {
        var header = $('header');
        if ($(this).width() < header.data('min-width')) {
            header.css('left', -$(this).scrollLeft());
        } else {
            header.css('left', '');
        }
    });

यह तब भी संभालना चाहिए जब आपका ब्राउज़र आपके हेडर की चौड़ाई से कम हो


0

मैंने सिर्फ स्थान जोड़ा: निरपेक्ष और जिसने मेरी समस्या हल कर दी।


8
पूर्ण स्थिति दस्तावेज़ के सापेक्ष तत्वों को ठीक करती है। निश्चित स्थिति उन्हें खिड़की के सापेक्ष ठीक करती है। यदि निरपेक्ष अपनी समस्या का उपयोग कर रहे हैं तो आप के साथ शुरू करने के लिए इस सवाल के रूप में एक ही समस्या नहीं थी।
नियाल

0

यह एक बहुत पुराना धागा है, लेकिन मैंने कुछ रचनात्मक नेस्टिंग का उपयोग करके इसका शुद्ध सीएसएस समाधान पाया है। मैं jQuery विधि का प्रशंसक नहीं था ...

यहाँ फिडल करें: https://jsfiddle.net/4Juv5jq/

<div id="wrapper">
    <div id="fixeditem">
        Haha, I am a header. Nah.. Nah na na na
    </div>
    <div id="contentwrapper">
        <div id="content">
            Lorem ipsum.....
        </div>
    </div>
</div>

#wrapper {
position: relative;
width: 100%;
overflow: scroll;
}

#fixeditem {
position: absolute;
}

#contentwrapper {
width: 100%;
overflow: scroll;
}

#content {
width: 1000px;
height: 2000px;
}

6
आपके समाधान के लिए वास्तविक विंडो के बजाय कंटेंट के कंटेनर डिव को स्क्रॉल करना आवश्यक है, जो आदर्श से बहुत दूर है - आपका फिडेल क्यों प्रदर्शित करता है। आपकी सामग्री पृष्ठ के दाईं ओर से चल रही है, लेकिन क्षैतिज स्क्रॉलबार स्क्रीन पर नहीं है। यह एक बड़ा प्रयोज्य मुद्दा है।
Niall

0

प्रारंभ स्थिति की जांच करने के लिए स्क्रिप्ट अपडेट किया गया:

function float_horizontal_scroll(id) {
var el = jQuery(id);
var isLeft = el.css('left') !== 'auto';
var start =((isLeft ? el.css('left') : el.css('right')).replace("px", ""));
jQuery(window).scroll(function () {
    var leftScroll = jQuery(this).scrollLeft();
    if (isLeft)
        el.css({ 'left': (start + leftScroll) + 'px' });
    else
        el.css({ 'right': (start - leftScroll) + 'px' });
});

}



0

पैरेंट डिव पर आप जोड़ सकते हैं

overflow-y: scroll; 
overflow-x: hidden;

0
$(window).scroll(function(){
    $('#header').css({
        'left': $(this).scrollLeft() + 15 
         //Why this 15, because in the CSS, we have set left 15, so as we scroll, we would want this to remain at 15px left
    });
});

धन्यवाद


0

हाँ, आप वास्तव में केवल CSS का उपयोग करके कर सकते हैं ...

body { width:100%; margin-right: 0; margin-left:0; padding-right:0; padding-left:0; }

मुझे बताओ अगर यह काम करता है



-3

लगता है कि आपको स्थिति का उपयोग करने की आवश्यकता है: निश्चित और फिर शीर्ष स्थिति को एक प्रतिशत पर सेट करें और एक निश्चित इकाई के लिए या तो बाईं या दाईं स्थिति।

हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.